Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions examples/errors_exception_direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
12 changes: 11 additions & 1 deletion src/exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 '';
}
Expand Down