Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows Errors in $router->exceptionRoute method #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions library/Respect/Rest/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ protected function catchExceptions($e)
}
}

/**
* Does a catch-like operation on an error based on previously
* declared instances from Router::exceptionRoute
*
* @param Error $e Any exception
*
* @return mixed A route forwarding or a silent null
*/
protected function catchErrors($e)
{
foreach ($this->route->sideRoutes as $sideRoute) {
$errorClass = get_class($e);
if (
$errorClass === $sideRoute->class
|| $sideRoute->class === 'Error'
|| $sideRoute->class === '\Error'
) {
$sideRoute->exception = $e;

return $this->forward($sideRoute);
}
}
}

/**
* Generates and returns the response from the current route
*
Expand Down Expand Up @@ -256,6 +280,14 @@ public function response()

//Returns whatever the exception routes returned
return (string) $exceptionResponse;
} catch (\Error $e) {
//Tries to catch it using catchErrors()
if (!$errorResponse = $this->catchErrors($e)) {
throw $e;
}

//Returns whatever the exception routes returned
return (string) $errorResponse;
}
}

Expand Down