Class ResultResponseMessage
Class with the default and consolidated result of the request.
Responsible for encapsulating validations and responses with specific methods that aid in the handling of messages.
Namespace: StoneCo.Framework.Services
Assembly: cs.temp.dll.dll
Syntax
[Serializable]
public class ResultResponseMessage : IActionResult
Constructors
ResultResponseMessage()
Default constructor.
warning
This constructor is only for the internal use of the framework.
Declaration
public ResultResponseMessage()
ResultResponseMessage(RequestMessage)
Mandatory constructor that set HttpStatusCode 200 and Message Success.
note
Request information is required to configure the response. Always use this constructor by default.
Declaration
public ResultResponseMessage(RequestMessage requestMessage)
Parameters
Type | Name | Description |
---|---|---|
RequestMessage | requestMessage | Request that will be referenced in the response. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public IActionResult CreateNew(RequestMessage request)
{
return new ResultResponseMessage(request);
}
}
Properties
HttpStatusCode
Result ststus code. This code follows the HttpStatusCode pattern.
Declaration
public HttpStatusCode HttpStatusCode { get; }
Property Value
Type | Description |
---|---|
HttpStatusCode | Sample 400, 500, 200, ... |
Message
Validation message.
Declaration
public string Message { get; }
Property Value
Type | Description |
---|---|
System.String | Sample Error on service layer., Success, ... |
Tracekey
Key of trace.
Declaration
public string Tracekey { get; }
Property Value
Type | Description |
---|---|
System.String | Sample a4477c7f88264bc1973e55856c8cfbd2 |
Validations
Validations of the properties.
Declaration
public IEnumerable<ValidationMessage> Validations { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<ValidationMessage> | Validation items if they exist, otherwise, null. |
Methods
AddValidation(IEnumerable<ValidationMessage>)
Add collection of validations the list of validations.
Declaration
public void AddValidation(IEnumerable<ValidationMessage> validations)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<ValidationMessage> | validations | The collection of validations. |
AddValidation(String, String)
Add validated field in the list of validations.
Declaration
public void AddValidation(string attribute, string message)
Parameters
Type | Name | Description |
---|---|---|
System.String | attribute | The field name validated. |
System.String | message | The message that describes the field validation. |
CreateResponseAccepted(String)
202 Accepted
Used to process in the background.
Create HttpStatusCode 202 and Message Processing in background.
Declaration
public void CreateResponseAccepted(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseAccepted();
return await result;
}
}
CreateResponseBadGateway(String)
502 Bad Gateway
An HTTP 502 - bad gateway server error response code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.
Create HttpStatusCode 502 and Message by parameter, otherwise, the message Service Unavailable.
is setted as default.
note
Used primarily by connectors provided by Enterprise Service Bus or clients provided by Domains and Microservices.
Declaration
public void CreateResponseBadGateway(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseServiceUnavailable();
return await result;
}
}
CreateResponseBadRequest(String)
400 Bad Request
Used in most errors that occur in an HTTP request.
Create HttpStatusCode 400 and Message by parameter, otherwise, the message Validation
is setted as default.
note
It is common to be used for field validation.
Declaration
public void CreateResponseBadRequest(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseBadRequest("Field validation.");
return await result;
}
}
CreateResponseConflict(String)
409 Conflict
The request could not be completed due to a conflict with the current state of the target resource.
Create HttpStatusCode 409 and Message by parameter, otherwise, the message Timeout reached on request.
is setted as default.
note
Used exclusively by clients provided by Domains and Microservices.
Declaration
public void CreateResponseConflict(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseConflict();
return await result;
}
}
CreateResponseCreated()
201 Created
Used to create a new entry in the database.
Create HttpStatusCode 201 and Message Created successfully.
Declaration
public void CreateResponseCreated()
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseCreated();
return await result;
}
}
CreateResponseForbidden()
403 Forbidden
Used for HTTP requests rejected by the server.
Create HttpStatusCode 403 and Message The server refused the request.
note
Used primarily by connectors provided by Enterprise Service Bus or clients provided by Domains and Microservices.
Declaration
public void CreateResponseForbidden()
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseForbidden();
return await result;
}
}
CreateResponseGatewayTimeout(String)
504 GatewayTimeout
Used to inform you that a service is unavailable.
Create HttpStatusCode 504 and Message by parameter, otherwise, the message Timeout reached at gateway.
is setted as default.
note
Used exclusively by connectors provided by Enterprise Service Bus.
Declaration
public void CreateResponseGatewayTimeout(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseGatewayTimeout();
return await result;
}
}
CreateResponseInternalServerError(String)
500 Internal Server Error
Used for internal server errors or thrown exceptions.
Create HttpStatusCode 500 and Message by parameter, otherwise, the message Error on service layer.
is setted as default.
Declaration
public void CreateResponseInternalServerError(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
try
{
// Your code
}
catch(Exception ex)
{
// Log Error ex
result.CreateResponseInternalServerError();
}
return await result;
}
}
CreateResponseNoContent()
204 No Content
Used to update one or more entries in the database and no results are returned or to get entries and no results are returned with the search criteria.
Create HttpStatusCode 204 and Message No content on result.
Declaration
public void CreateResponseNoContent()
Examples
Bar class used in this example.
public class Bar
{
[HttpGet("{id}")]
public async Task<IActionResult> GetById(GetFooRequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseNoContent();
return await result;
}
}
CreateResponseNotAcceptable()
406 NotAcceptable
Used to report missing information in HTTP request header.
Create HttpStatusCode 406 and Message Header information is required.
note
Use Validations to specify what information is required in the header.
Declaration
public void CreateResponseNotAcceptable()
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.AddValidation("Tracekey", "Is required.");
result.CreateResponseNotAcceptable();
return await result;
}
}
CreateResponseNotFound()
404 Not Found
Used to inform you that the resource or route was not found.
Create HttpStatusCode 404 and Message Route or resource not found on server.
note
Used primarily by connectors provided by Enterprise Service Bus or clients provided by Domains and Microservices.
Declaration
public void CreateResponseNotFound()
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseNotFound();
return await result;
}
}
CreateResponseOk()
Declaration
public void CreateResponseOk()
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseOk();
return await result;
}
}
CreateResponseRequestTimeout(String)
408 RequestTimeout
Used to inform that the request timeout has been reached and the request has been lost.
Create HttpStatusCode 408 and Message by parameter, otherwise, the message Timeout reached on request.
is setted as default.
note
Used exclusively by clients provided by Domains and Microservices.
Declaration
public void CreateResponseRequestTimeout(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseRequestTimeout();
return await result;
}
}
CreateResponseServiceUnavailable(String)
503 Service Unavailable
Used to inform you that a service is unavailable.
Create HttpStatusCode 503 and Message by parameter, otherwise, the message Service Unavailable.
is setted as default.
note
Used primarily by connectors provided by Enterprise Service Bus or clients provided by Domains and Microservices.
Declaration
public void CreateResponseServiceUnavailable(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseServiceUnavailable();
return await result;
}
}
CreateResponseUnauthorized(String)
401 Unauthorized
Used for invalid token or access restrictions.
Create HttpStatusCode 401 and Message by parameter, otherwise, the message Unauthorized access to the resource.
is setted as default.
Declaration
public void CreateResponseUnauthorized(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("login")]
public async Task<IActionResult> Login(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseUnauthorized();
return await result;
}
}
CreateResponseUnprocessableEntity(String)
422 Unprocessable Entity
Used for business validation.
Create HttpStatusCode 422 and Message by parameter, otherwise, the message Validation
is setted as default.
note
When there are any rules that prevent the process from proceeding because of business rules, use this type of validation.
Declaration
public void CreateResponseUnprocessableEntity(string message = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | message | Validation message. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseUnprocessableEntity("The message does not meet the minimum requirements.");
return await result;
}
}
IsHttpStatusCodeError()
Check the result.
Declaration
public bool IsHttpStatusCodeError()
Returns
Type | Description |
---|---|
System.Boolean | True if HttpStatusCode less than 400, otherwise, false. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage(request);
result.CreateResponseUnauthorized();
if(result.IsHttpStatusCodeError())
// Your decision...
return await result;
}
}
MapResultResponseMessage(HttpStatusCode, String, String, IEnumerable<ValidationMessage>)
Set informations to this result.
Declaration
public void MapResultResponseMessage(HttpStatusCode httpStatusCode, string message = null, string traceKey = null, IEnumerable<ValidationMessage> validations = null)
Parameters
Type | Name | Description |
---|---|---|
HttpStatusCode | httpStatusCode | Contains the values of status codes defined for HTTP. |
System.String | message | Validation message. |
System.String | traceKey | Key of trace. |
System.Collections.Generic.IEnumerable<ValidationMessage> | validations | Validations of the properties. |
SetRequestMessage(RequestMessage)
Set the RequestMessage for this request.
Declaration
public void SetRequestMessage(RequestMessage requestMessage)
Parameters
Type | Name | Description |
---|---|---|
RequestMessage | requestMessage | Request that will be referenced in the response. |
Examples
Bar class used in this example.
public class Bar
{
[HttpPost("new")]
public async Task<IActionResult> CreateNew(RequestMessage request)
{
var result = new ResultResponseMessage();
result.SetRequestMessage(request);
return await result;
}
}
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If requestMessage is null, an exception is thrown with message |
System.ArgumentException | If Tracekey in the header is not valid, an exception is thrown with message |