{
function call($request)
{
+ $previous = set_error_handler(array($this, 'error_handler'));
+ register_shutdown_function(array($this, 'shutdown_handler'), $request);
try {
- $response = null;
- if ($this->next) {
- $response = $this->next->call($request);
- }
+ if (!$this->next)
+ throw new \Exception('Cannot process request. No next middleware is given.');
- if (!($response instanceof IPF_HTTP_Response)) {
- throw new \Exception('Response is not a IPF_HTTP_Response');
- }
+ $response = $this->next->call($request);
- return $response;
+ if (!($response instanceof IPF_HTTP_Response))
+ throw new \Exception('Response is not a IPF_HTTP_Response');
} catch (\Exception $exception) {
error_log($exception);
- return new IPF_HTTP_Response_ServerError($request, $exception);
+ $response = new IPF_HTTP_Response_ServerError($request, $exception);
+ }
+ set_error_handler($previous);
+ return $response;
+ }
+
+ function error_handler($errno, $errstr, $errfile, $errline)
+ {
+ if (error_reporting() & $errno)
+ throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
+ }
+
+ function shutdown_handler($request)
+ {
+ $error = error_get_last();
+ if ($error) {
+ $exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
+ $response = new IPF_HTTP_Response_ServerError($request, $exception);
+ $response->render();
}
}
}