Error Message Constants (ErrorConstants) Class
Namespace: Cxt.Account.Settings.API.Constants
The ErrorConstants class centralizes predefined string constants for error codes and messages, ensuring consistent API error reporting.
Purpose
- Standardization: Provides uniform error codes and messages for API responses.
- Decoupling: Avoids hardcoding error strings in business logic or controllers.
- Clarity: Offers a clear reference for API-producible errors.
- Simplicity: Eases construction of error responses.
Generic Error Constants
These constants define general error codes and their corresponding user-friendly messages.
| Constant Name | Value | Description |
|---|---|---|
ERROR_CODE_INVALID_REQUEST | InvalidRequest | The request syntax is invalid or cannot be fulfilled. |
ERROR_MESSAGE_INVALID_REQUEST | The request is invalid. | User-friendly message for an invalid request. |
ERROR_CODE_UNAUTHORIZED | Unauthorized | Authentication is required and has failed or not been provided. |
ERROR_MESSAGE_UNAUTHORIZED | You are not authorized to access this resource. | User-friendly message for unauthorized access. |
ERROR_CODE_FORBIDDEN | Forbidden | The server understood the request, but refuses to authorize it. |
ERROR_MESSAGE_FORBIDDEN | You do not have permission to access this resource. | User-friendly message for forbidden access. |
ERROR_CODE_NOT_FOUND | NotFound | The requested resource could not be found on the server. |
ERROR_MESSAGE_NOT_FOUND | The requested resource was not found. | User-friendly message when a resource is not found. |
ERROR_CODE_CONFLICT | Conflict | The request could not be completed due to a conflict with the current state of the resource. |
ERROR_MESSAGE_CONFLICT | There is a conflict with the current state of the resource. | User-friendly message for a resource conflict. |
ERROR_CODE_INTERNAL_SERVER_ERROR | InternalServerError | A generic error message for an unexpected server condition. |
ERROR_MESSAGE_INTERNAL_SERVER_ERROR | An internal server error occurred. | User-friendly message for an internal server error. |
ERROR_MESSAGE_GENERIC | An error occured while processing a request. | Default message for unspecific processing errors. |
Application-Specific Error Message Constants
These messages are tailored for operations within the Account Settings API.
| Constant Name | Value | Description |
|---|---|---|
ERROR_MESSAGE_ON_ACCOUNT_ADD | One or more account settings were not added successfully. | Indicates failure during a batch add operation for account settings. |
ERROR_MESSAGE_ON_ACCOUNT_UPDATE | One or more account settings were not updated successfully. | Indicates failure during an update operation for one or more account settings. |
ERROR_MESSAGE_ON_ACCOUNT_DELETE | One or more account settings were not delete successfully. | Indicates failure during a delete operation for account settings. |
ERROR_MESSAGE_NO_ACCOUNTS_FOUND | No account settings found. | Used when a query for account settings returns no results for a client. |
Usage
These constants are typically used in API controllers or error handling middleware to construct standardized error responses (e.g., populating a BaseResponseModel or similar structure).
Example (Conceptual C# in an API Controller):
csharp
using Microsoft.AspNetCore.Mvc;
// Assuming BaseResponseModel and ErrorConstants are accessible
[ApiController]
[Route("api/[controller]")]
public class SomeController : ControllerBase
{
// ... other dependencies ...
[HttpPost("settings")]
public IActionResult AddAccountSettings(object settingsRequest)
{
bool success = false; // Replace with actual service call result
var response = new BaseResponseModel(); // Your standard response model
// ... attempt to add settings ...
if (!success)
{
response.Success = false;
response.Errors.Add(new ErrorModel // Assuming an ErrorModel structure
{
Code = ErrorConstants.ERROR_CODE_INVALID_REQUEST, // Or a more specific code
Message = ErrorConstants.ERROR_MESSAGE_ON_ACCOUNT_ADD
});
return BadRequest(response); // Or appropriate HTTP status code
}
response.Success = true;
// response.Data = ...; // Set data on success
return Ok(response);
}
}
// Dummy BaseResponseModel for context
public class BaseResponseModel
{
public bool Success { get; set; }
public List<ErrorModel> Errors { get; set; } = new List<ErrorModel>();
public object Data { get; set; }
}
// Dummy ErrorModel for context
public class ErrorModel
{
public string Code { get; set; }
public string Message { get; set; }
}Using these constants promotes consistent and predictable error reporting, benefiting API client integration.