JSON API Error Object

JsonApiErrorObject class

Implementation of JSON API Error Object Specification. See more: http://jsonapi.org/format/#error-objects


// creating a new Error Object:
$error = new JsonApiErrorObject();

// set some known data about the error:
$error->setTitle(new LangText('LBL_ERROR_LABEL_TITLE'));
$error->setDetail(new LangText('LBL_ERROR_LABEL_DETAIL'));
// etc.. see more in source and phpDoc

// creating new Error Object with known data directly in constructor, all parameters are optional:
$error = new JsonApiErrorObject(
    // title: a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
    new LangText('LBL_ERROR_LABEL_TITLE'),
    // detail: a human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
    new LangText('LBL_ERROR_LABEL_DETAIL'),
    // id: a unique identifier for this particular occurrence of the problem.
    // code: an application-specific error code, expressed as a string value.
    // status: the HTTP status code applicable to this problem, expressed as a string value.
    // links: a links object
    ['about' => 'Descrioption about the problem'],
    // source: an object containing references to the source of the error
    ['pointer' => '/test/foo/bar', 'parameter' => 'wrong'],
    // meta: a meta object containing non-standard meta-information about the error.
    ['some' => 'meta info']

// retriving Error Object from an Exception:
try {
  // ...do stuff
} catch (Exception $e) {

// retrieving Error object from a ServerRequestInterface:

// converting to an array:
$array = $error->export();

// converting to a JSON string:
$json = $error->exportJson();

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.