diff --git a/examples/errors_exception_direct.php b/examples/errors_exception_direct.php index 5a42255..1c5d447 100644 --- a/examples/errors_exception_direct.php +++ b/examples/errors_exception_direct.php @@ -13,7 +13,6 @@ * @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 - * @note echo'ing the exception has the same effect as using ->send_response() */ try { @@ -22,7 +21,6 @@ $about_link = 'www.example.com/search'; throw new jsonapi\exception('unknown user', $http_status, $previous=null, $friendly_message, $about_link); } -catch (Exception $e) { - echo $e; - exit; +catch (jsonapi\exception $e) { + $e->send_response(); } diff --git a/src/exception.php b/src/exception.php index d1f5468..b60a410 100644 --- a/src/exception.php +++ b/src/exception.php @@ -34,6 +34,9 @@ class exception extends \Exception { * i.e. a link to the api documentation */ public function __construct($message='', $code=0, $previous=null, $friendly_message=null, $about_link=null) { + // exception is the only class not extending base + new base(); + parent::__construct($message, $code, $previous); if ($friendly_message) { @@ -70,16 +73,23 @@ public function set_about_link($about_link) { public function send_response($content_type=null, $encode_options=null, $response=null) { $jsonapi = new errors($this, $this->friendly_message, $this->about_link); $jsonapi->send_response($content_type, $encode_options, $response); - exit; + exit; // sanity check } /** * alias for ->send_response() * + * @deprecated as this causes hard to debug issues .. + * .. when exceptions are called as a by-effect of this function + * * @return string empty for sake of correctness * as ->send_response() already echo's the json and terminates script execution */ public function __toString() { + if (base::$debug) { + trigger_error('toString conversion of exception is deprecated, use ->send_response() instead', E_USER_DEPRECATED); + } + $this->send_response(); return ''; }