Início / Optitravel / Helpers / ApiHelper
error(string $message, int $code = 400, $details = null, array $errors = [], string $protocol = '')
- error(string $message, int $code = 400, $details = null, array $errors = [], string $protocol = '')
- Parameters
- Example
This method is used to send a standardized JSON error response. It sets the appropriate HTTP status code, content type, and halts execution after returning the error message. Optionally, it supports protocol-specific formatting (e.g., SCIM).
Use this method when an API operation fails due to validation, missing data, or logical errors.
Parameters
-
$message – (string) Required. A human-readable description of the error.
-
$code – (int) Optional. The HTTP status code (default is 400).
-
$details – (string|null) Optional. Additional information about the error.
-
$errors – (array) Optional. A structured list of field-specific or logical errors.
-
$protocol – (string) Optional. If set to 'SCIM', the response is formatted according to SCIM specifications.
Example
use \App\Utils\ApiHelper;
$errors = [
'FIELD_REQUIRED' => [
['field' => 'email', 'description' => 'The email field is required.']
]
];
ApiHelper::error('Validation failed', 400, 'One or more fields are missing.', $errors);
Output default protocol:
{
"error": true,
"code": 400,
"message": "Validation failed",
"details": "One or more fields are missing.",
"errors": {
"FIELD_REQUIRED": [
{
"field": "email",
"description": "The email field is required."
}
]
}
}
Example Output (SCIM protocol):
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:Error"
],
"scimType": "invalidValue",
"detail": "The email field is required.",
"status": 400
}
↑ (Última atualização: 06/05/2025)