Skip to content

Error Functions

Thieres Tembra edited this page Jun 29, 2016 · 3 revisions

As you probably have noticed the error functions that this package provides can receive one or multiple error codes or error objects. This or these error codes are the first parameter of these functions, the only one that is required.

The second optional parameter tells the package whether a JsonApiException should be thrown. The default is true. If you provide this parameter as false, no exception will be thrown and the function will return a JSON string representation of the errors.

echo MyJsonApiErrors::badRequest([
  827 => [
    'title' => 'Another Error',
    'detail' => 'Detailed error description'
  ]
], false);

will output as string

{
  "errors": [
    {
      "status": "400",
      "code": "827",
      "title": "Another Error",
      "detail": "Detailed error description"
    }
  ]
}

Available error functions

This package provides the most common HTTP Status Codes as error functions:

  • badRequest: Returns a 400 bad request error.
  • unauthorized: Returns a 401 unauthorized error.
  • forbidden: Returns a 403 forbidden error.
  • notFound: Returns a 404 not found error.
  • methodNotAllowed: Returns a 405 method not allowed error.
  • internal: Returns a 500 internal server error.

Generic error function

This package also provides a generic error function that has three parameters:

  1. The first parameter is the same error codes of other functions.
  2. The second optional parameter is the HTTP Status Code that you want to return. The default is 500.
  3. The third optional parameter tells the package whether a JsonApiException should be thrown. The default is true. It is the same as second optional parameter of other functions.

The example below will return a HTTP 503 Service Unavailable error:

MyJsonApiErrors::error([
  827 => [
    'title' => 'Unavailable',
    'detail' => 'The server is currently unable to handle the request.'
  ]
], 503);

← Raising Application Errors | Localization →

Clone this wiki locally