Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit cdf576c

Browse files
maurice2kMoritz Fain
authored andcommitted
Added support for PHP7 \Throwable based exceptions
1 parent 6edfedf commit cdf576c

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

src/DispatchListener.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public function onDispatch(MvcEvent $e)
9898
} catch (InvalidServiceException $exception) {
9999
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_INVALID, $controllerName, $e, $application, $exception);
100100
return $this->complete($return, $e);
101+
} catch (\Throwable $exception) {
102+
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
103+
return $this->complete($return, $e);
101104
} catch (\Exception $exception) {
102105
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
103106
return $this->complete($return, $e);
@@ -109,15 +112,22 @@ public function onDispatch(MvcEvent $e)
109112

110113
$request = $e->getRequest();
111114
$response = $application->getResponse();
115+
$caughtException = null;
112116

113117
try {
114118
$return = $controller->dispatch($request, $response);
119+
} catch (\Throwable $ex) {
120+
$caughtException = $ex;
115121
} catch (\Exception $ex) {
122+
$caughtException = $ex;
123+
}
124+
125+
if ($caughtException !== null) {
116126
$e->setName(MvcEvent::EVENT_DISPATCH_ERROR);
117127
$e->setError($application::ERROR_EXCEPTION);
118128
$e->setController($controllerName);
119129
$e->setControllerClass(get_class($controller));
120-
$e->setParam('exception', $ex);
130+
$e->setParam('exception', $caughtException);
121131

122132
$return = $application->getEventManager()->triggerEvent($e)->last();
123133
if (! $return) {
@@ -135,7 +145,7 @@ public function reportMonitorEvent(MvcEvent $e)
135145
{
136146
$error = $e->getError();
137147
$exception = $e->getParam('exception');
138-
if ($exception instanceof \Exception) {
148+
if ($exception instanceof \Exception || $exception instanceof \Throwable) {
139149
zend_monitor_custom_event_ex($error, $exception->getMessage(), 'Zend Framework Exception', ['code' => $exception->getCode(), 'trace' => $exception->getTraceAsString()]);
140150
}
141151
}

src/MiddlewareListener.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ public function onDispatch(MvcEvent $event)
5656
$event->setResult($return);
5757
return $return;
5858
}
59+
60+
$caughtException = null;
5961
try {
6062
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
63+
} catch (\Throwable $exception) {
64+
$caughtException = $exception;
6165
} catch (\Exception $exception) {
66+
$caughtException = $exception;
67+
}
68+
69+
if ($caughtException !== null) {
6270
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
6371
$event->setError($application::ERROR_EXCEPTION);
6472
$event->setController($middlewareName);

src/View/Console/ExceptionStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function prepareExceptionViewModel(MvcEvent $e)
184184
if (is_callable($this->message)) {
185185
$callback = $this->message;
186186
$message = (string) $callback($exception, $this->displayExceptions);
187-
} elseif ($this->displayExceptions && $exception instanceof \Exception) {
187+
} elseif ($this->displayExceptions && ($exception instanceof \Exception || $exception instanceof \Throwable)) {
188188
$previous = '';
189189
$previousException = $exception->getPrevious();
190190
while ($previousException) {

src/View/Console/RouteNotFoundStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ protected function reportNotFoundReason($e)
460460
];
461461
$report = sprintf("\nReason for failure: %s\n", $reasons[$reason]);
462462

463-
while ($exception instanceof \Exception) {
463+
while ($exception instanceof \Exception || $exception instanceof \Throwable) {
464464
$report .= sprintf("Exception: %s\nTrace:\n%s\n", $exception->getMessage(), $exception->getTraceAsString());
465465
$exception = $exception->getPrevious();
466466
}

src/View/Http/DefaultRenderingStrategy.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,17 @@ public function render(MvcEvent $e)
9999
$view->setRequest($request);
100100
$view->setResponse($response);
101101

102+
$caughtException = null;
103+
102104
try {
103105
$view->render($viewModel);
106+
} catch (\Throwable $ex) {
107+
$caughtException = $ex;
104108
} catch (\Exception $ex) {
109+
$caughtException = $ex;
110+
}
111+
112+
if ($caughtException !== null) {
105113
if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) {
106114
throw $ex;
107115
}

src/View/Http/RouteNotFoundStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected function injectException($model, $e)
250250
$model->setVariable('display_exceptions', true);
251251

252252
$exception = $e->getParam('exception', false);
253-
if (!$exception instanceof \Exception) {
253+
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
254254
return;
255255
}
256256

0 commit comments

Comments
 (0)