Skip to content

Commit 4f21ad6

Browse files
Catch third party exceptions and return as BaseException with HTTP code of 500. Report the original exception's code within the error message
1 parent 625d1fb commit 4f21ad6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/Core/PlanckApp.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public static function run($router, $container, $config) {
2727
// set exception handler
2828
set_exception_handler(function ($exception) use ($response, $container) {
2929
// get the error response builder
30-
$exception->setErrorResponseBuilder($container->get('errorResponseBuilder'));
30+
if (method_exists($exception, 'setErrorResponseBuilder')) {
31+
$exception->setErrorResponseBuilder($container->get('errorResponseBuilder'));
32+
} else {
33+
$exception = new BaseException($exception->getMessage() . ' Exception code given: ' . $exception->getCode());
34+
}
3135

3236
// set the response body
3337
// the status, headers and other response properties might be modified in the errorResponseBuilder
@@ -57,6 +61,7 @@ public static function run($router, $container, $config) {
5761
// TODO: Maybe wrap this sort of thing in an invoke() function? Then can check object,
5862
// check method exists, handle errors more generally and gracefully?
5963
$routingData = call_user_func_array([$router, $config['app.router.handler']], []);
64+
6065
$controller = $routingData['controller'] . 'Controller';
6166
$action = either($routingData['action'], 'index');
6267
$params = either($routingData['vars'], []);

0 commit comments

Comments
 (0)