/** * adds a jsonapi\error object to the errors collection * used internally by ->add_error(), ->fill_errors() and ->add_exception() * * further, a generic http status is gathered from added objects * * @param jsonapi\error $error */ private function add_error_object(\alsvanzelf\jsonapi\error $error) { $error_response_part = $error->get_array(); $error_http_status = $error->get_http_status(); $this->errors_collection[] = $error_response_part; if ($error_http_status) { $this->set_http_status($error_http_status); } }
<?php use alsvanzelf\jsonapi; ini_set('display_errors', 1); error_reporting(-1); require '../vendor/autoload.php'; /** * setting all options */ $error = new jsonapi\error($error_message = 'too much options', $friendly_message = 'Please, choose a bit less.', $about_link = 'www.example.com/options.html'); // more details about the error, for the end user to comsume $error->set_friendly_detail($friendly_detail = 'Consult your ...'); // mark the cause of the error $error->blame_post_body($post_body_pointer = '/data/attributes/title'); $error->blame_get_parameter($get_parameter_name = 'filter'); // an identifier useful for helpdesk purposes $error->set_identifier($identifier = 42); // add meta data as you would on a normal json response $error->add_meta($key = 'foo', $meta_data = 'bar'); $error->fill_meta($meta_data = ['bar' => 'baz']); // or as object $meta_object = new stdClass(); $meta_object->property = 'value'; $error->add_meta($key = 'object', $meta_object); // the http status code // @note it is better to set this on the jsonapi\errors object .. // .. as only a single one can be consumed by the browser $error->set_http_status($http_status = jsonapi\response::STATUS_NOT_FOUND); // if not set during construction, set them here $error->set_error_message($error_message = 'too much options'); $error->set_friendly_message($friendly_message = 'Please, choose a bit less.');