ApiErrorResponse - viames/pair GitHub Wiki

Pair framework: ApiErrorResponse

Pair\Api\ApiErrorResponse is the explicit response object for standardized API errors.

Use it when you want to return a ResponseInterface instance instead of terminating the request immediately through ApiResponse::error().

Constructor

new ApiErrorResponse(string $errorCode, string $errorMessage, int $httpCode = 400, array $extra = [])

Arguments:

  • errorCode: stable machine-readable code
  • errorMessage: human-readable error message
  • httpCode: HTTP status code
  • extra: additional payload fields merged into the JSON body

Example:

use Pair\Api\ApiErrorResponse;

return new ApiErrorResponse('BAD_REQUEST', 'Invalid payload', 422, [
    'detail' => 'Missing field',
]);

Emitted payload:

{
  "code": "BAD_REQUEST",
  "error": "Invalid payload",
  "detail": "Missing field"
}

Notes

  • Non-string keys in $extra are ignored.
  • send() delegates to the JSON response path, so the runtime behavior stays aligned with other explicit Pair v4 responses.
  • If you want to reuse the built-in/custom ApiResponse error registry, prefer ApiResponse::errorResponse().

See also: ApiResponse, API, Request.