Skip to main content

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​

PropertyTypeDescription
ErrorCodestring?Machine-readable code for client-side handling. Omitted from JSON when null.
ErrorMessagestringHuman-readable description of the error. Omitted from JSON when null.
Nominally non-nullable ErrorMessage

ErrorMessage 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​

TypeAddsUsed by
BadRequestResultPropertyName, AttemptedValue, ExpectedValueBadRequestResponse
NotFoundResultEntity, ValueNotFoundResponse
ConflictResultEntity, ValueConflictResponse

Example JSON Output​

{
"errorCode": "PROCESSING_ERROR",
"errorMessage": "An unexpected error occurred."
}