Skip to content

Raising Application Errors

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

Raise an application error is as easiest as calling one of static functions that Mytdt\JsonApi\Errors\MyJsonApiErrors provides.

Direct call

If you call a function directly from Mytdt\JsonApi\Errors\MyJsonApiErrors means that you don't have an Application Error Codes class, so you will need to provide the entire associative array that represents a JSON API Error Objects.

MyJsonApiErrors::badRequest([
  10 => [
    'title' => 'Invalid Parameter',
    'detail' => 'The parameter given is invalid.'
  ]
]);
MyJsonApiErrors::internal([
  10 => [
    'title' => 'Invalid Parameter',
    'detail' => 'The parameter given is invalid.'
  ],
  20 => [
    'title' => 'Update Failed',
    'detail' => 'Can not update the resource.',
    'method' => 'dataId'
  ]
]);

Calling from an Application Error Code class

If you call a function from your own class (e.g. AppErrorCodes) that extends Mytdt\JsonApi\Errors\MyJsonApiErrors all you need is to provide the error code that you have defined before on your class as constants.

AppErrorCodes::badRequest(AppErrorCodes::INVALID_PARAMETER);

You can also raise multiple errors by providing an array of codes.

AppErrorCodes::internal([
  AppErrorCodes::INVALID_PARAMETER,
  AppErrorCodes::UPDATE_FAILED,
]);

Finally you can provide/override any JSON API Error Objects member that you haven't/have defined on your own class for this particular occurrence of problem you are raising.

AppErrorCodes::internal([
  AppErrorCodes::INVALID_PARAMETER => [
    'parameter' => 'page'
  ],
  AppErrorCodes::UPDATE_FAILED => [
    'links' => [
      'href' => 'http://example.com/why/update/failed'
    ]
  ]
]);

However keep in mind that some members like title should not change from occurrence to occurrence of the problem, except for purposes of localization, like stated on JSON API Error Objects specification version 1.0.

← JSON API Error Objects | Error Functions →

Clone this wiki locally