UnprocessableResponse
Namespace: RA.Utilities.Api.Response
The UnprocessableResponse class creates a standardized 422 Unprocessable Entity response. It derives from Response<T> with T = ErrorResult and is used when the request is syntactically valid but semantically impossible.
🎯 Purpose
Use UnprocessableResponse for business-rule violations that are not field validation errors — for example, cancelling an order that has already shipped.
- Standard envelope:
ResponseCode = 422,ResponseType = ResponseType.Unprocessable,ResponseMessage = "Unprocessable entity." - Optional payload:
Resultcan benullor anErrorResultdescribing the semantic failure.
Constructors
| Constructor | Result | ResponseCode | ResponseMessage |
|---|---|---|---|
UnprocessableResponse() | null | 422 | "Unprocessable entity." |
UnprocessableResponse(int responseCode, string responseMessage = "") | null | responseCode | responseMessage ?? standard (the "" default is used as-is) |
UnprocessableResponse(ErrorResult result) | result, or a default ErrorResult when null | 422 | "Unprocessable entity." |
UnprocessableResponse(ErrorResult result, int responseCode, string responseMessage = "") | result, or a default ErrorResult when null | responseCode | responseMessage ?? standard (the "" default is used as-is) |
ResponseType is always ResponseType.Unprocessable (JSON 13). When result is null, the constructor substitutes an ErrorResult with ErrorCode = "Unprocessable" and ErrorMessage = "Unprocessable entity.".
Usage
using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Api.Response;
[HttpPost("{id}/cancel")]
public IActionResult CancelOrder(int id)
{
var order = _service.GetById(id);
if (order.Status == OrderStatus.Shipped)
{
return UnprocessableEntity(new UnprocessableResponse(new ErrorResult
{
ErrorCode = "ORDER_ALREADY_SHIPPED",
ErrorMessage = "Cannot cancel an order that has already been shipped."
}));
}
_service.Cancel(id);
return Ok(new SuccessResponse<string>("Order cancelled."));
}
Example JSON Output
{
"responseCode": 422,
"responseType": 13,
"responseMessage": "Unprocessable entity.",
"result": {
"errorCode": "ORDER_ALREADY_SHIPPED",
"errorMessage": "Cannot cancel an order that has already been shipped."
}
}
With JsonStringEnumConverter, responseType is "Unprocessable".