ErrorResult
Namespace: RA.Utilities.Api.Response
The ErrorResult class is the base payload for structured errors. It is used directly by ErrorResponse, UnauthorizedResponse, PaymentRequiredResponse, ForbiddenResponse, UnprocessableResponse, TooManyRequestsResponse, ServiceUnavailableResponse, and GatewayTimeoutResponse, and it is the base class for BadRequestResult, NotFoundResult, and ConflictResult.
🎯 Purpose
ErrorResult gives error responses two standard fields: a machine-readable ErrorCode and a human-readable ErrorMessage. Both are optional on the wire and are omitted when null.
Class Definition
using System.Text.Json.Serialization;
public class ErrorResult
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ErrorCode { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string ErrorMessage { get; set; }
}
Properties
| Property | Type | Description |
|---|---|---|
| ErrorCode | string? | Machine-readable code for client-side handling. Omitted from JSON when null. |
| ErrorMessage | string | Human-readable description of the error. Omitted from JSON when null. |
Nominally non-nullable
ErrorMessageErrorMessage is declared as string, but the class does not initialize it. new ErrorResult() therefore leaves it null at runtime; always set it when you return the object:
var error = new ErrorResult
{
ErrorCode = "PROCESSING_ERROR",
ErrorMessage = "An unexpected error occurred."
};
Usage
using RA.Utilities.Api.Response;
var error = new ErrorResult
{
ErrorCode = "PROCESSING_ERROR",
ErrorMessage = "An unexpected error occurred."
};
return StatusCode(500, new ErrorResponse(error));
Derived Types
| Type | Adds | Used by |
|---|---|---|
BadRequestResult | PropertyName, AttemptedValue, ExpectedValue | BadRequestResponse |
NotFoundResult | Entity, Value | NotFoundResponse |
ConflictResult | Entity, Value | ConflictResponse |
Example JSON Output
{
"errorCode": "PROCESSING_ERROR",
"errorMessage": "An unexpected error occurred."
}