JSON API Error Object

JsonApiErrorObject class

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

Usage

// 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'));
$error->setCode(ERROR_CODE);
// 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.
    ERROR_ID,
    // code: an application-specific error code, expressed as a string value.
    ERROR_CODE,
    // status: the HTTP status code applicable to this problem, expressed as a string value.
    STATUS,
    // 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) {
  $error->retrieveFromException($e)
}

// retrieving Error object from a ServerRequestInterface:
$error->retrieveFromRequest($request);

// 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.