UnauthorizedResponse
Namespace: RA.Utilities.Api.Results
The UnauthorizedResponse class is a specialized Data Transfer Object (DTO) designed to create a standardized and consistent JSON response body whenever your API needs to return an HTTP 401 Unauthorized error.
Its main goals are:
1. Standardization:
It ensures that every 401 error response from your API has the exact same structure.
It inherits from the base Response<T> class, so it includes common fields like ResponseCode, ResponseMessage, and ResponseType.
2. Semantic Meaning:
It sets the ResponseType property to ResponseType.Unauthorized.
As shown in your documentation, this provides a clear, machine-readable signal in the JSON payload that goes beyond the HTTP status code, telling the client exactly what kind of error occurred.
3. Simplicity:
It encapsulates the logic for creating a 401 response body, so your error handling code doesn't need to build it manually.
🛠️ How It's Used in Your Application
The class is a key part of your API's exception handling pipeline:
- Some part of your application throws an
UnauthorizedException. - The
GlobalExceptionHandler(not shown, but implied by the structure) catches this exception. - It calls
ErrorResultResponse.Result(exception). - The switch expression in
ErrorResultResponsematches theUnauthorizedExceptionand callsErrorResultMapper.MapToUnauthorizedResponse(exception). - This mapper creates an instance of your
UnauthorizedResponseclass, populating it with the correct code (401) and message. - Finally, this
UnauthorizedResponseobject is serialized into a JSON string and sent to the client with an HTTP 401 status code.
Example JSON Output
{
"responseCode": 401,
"responseType": "Unauthorized",
"responseMessage": "User is not authorized to perform this action.",
"result": null
}