<?php use alsvanzelf\jsonapi; ini_set('display_errors', 1); error_reporting(-1); require '../vendor/autoload.php'; /** * via an exception * * @note previous exceptions will be added as well * @note exceptions only output file, line, trace if the display_errors directive is true * you can tune it with that, or by setting jsonapi\base::$debug to false */ try { throw new Exception('unknown user', jsonapi\response::STATUS_NOT_FOUND); } catch (Exception $e) { $jsonapi = new jsonapi\errors($e); $jsonapi->send_response(); }
// @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.'); $error->set_about_link($about_link = 'www.example.com/options.html'); /** * prepare multiple error objects for the errors response */ $another_error = new jsonapi\error('kiss', 'Error objects can be small and simple as well.'); $some_exception = new Exception('please don\'t throw things', jsonapi\response::STATUS_INTERNAL_SERVER_ERROR); /** * building up the json response * * you can pass the $error object to the constructor .. * .. or add multiple errors via ->add_error() or ->add_exception() * * further you can force another http status code than what's in the errors */ $jsonapi = new jsonapi\errors($error); $jsonapi->add_error($another_error); $jsonapi->add_exception($some_exception); $jsonapi->set_http_status(jsonapi\response::STATUS_BAD_REQUEST); /** * sending the response * * @note the response includes debug information based on the display_errors directive * you can tune it with that, or by setting jsonapi\base::$debug to false */ $jsonapi->send_response();