Standardize API responses with `ResponseModel`
mainUse ResponseModel to ensure that every API response (both successful and error states) follows a consistent JSON structure. This makes your API predictable for frontend consumers and simplifies OpenAPI/Swagger documentation.
Every response generated via ResponseModel includes the following fields:
data: The actual payload (populated on success,nullon failure).status: AnExceptionStatusvalue (e.g.,SUCCESS,WARNING,FAIL).message: A short summary of the outcome.error_code: A specific error identifier (populated only if there is an error).description: Extra context for debugging.
Interpretation Logic:
- Success:
datais populated anderror_codeisnull. - Failure:
error_codeis populated. - Frontend Logic: Use the
statusfield as the primary driver for application logic.
# Example of the resulting JSON structure
{
"data": { "api_key": "valid_key" },
"status": "SUCCESS",
"message": "API key is valid",
"error_code": null,
"description": "The provided API key is valid."
}