/** * Handle exception * * @param \Exception $e * @return \Psr\Http\Message\ResponseInterface */ public function handle(\Exception $e) { $response = $this->provider->getResponse(); $response = $response->withStatus(500); $message = 'Internal Server Error'; $view = $this->provider->getView(); if ($this->provider->getConfig('display_exceptions')) { $view->exception = $e; } $view->message = $message; $config = $this->provider->getConfig(); if (isset($config['templates']['template_error'])) { $result = $view->renderFile($config['templates']['template_error']); } else { if (isset($view->exception)) { $body = sprintf("<h1>An error occurred</h1><h2>%s</h2><h3>Message</h3><p>%s</p><h3>Stack trace</h3><p>%s</p>", $message, $e->getMessage(), $e->getTraceAsString()); } else { $body = sprintf("<h1>An error occurred</h1><h2>%s</h2>", $message); } $result = sprintf("<html><head><title>%s</title><style>body {font-family: Helvetica,Arial,sans-serif;font-size: 20px;line-height: 28px;padding:20px;}</style></head><body>%s</body></html>", 'Tlumx application: ' . $message, $body); } $response->getBody()->write($result); $response->withHeader('Content-Type', 'text/html'); return $response; }
/** * Handle * * @param array $allowedMethods * @return \Psr\Http\Message\ResponseInterface */ public function handle(array $allowedMethods = []) { $response = $this->provider->getResponse(); if (empty($allowedMethods)) { $response = $response->withStatus(404); $message = 'Page not found'; } else { $response = $response->withStatus(405); $message = 'Method Not Allowed'; } $config = $this->provider->getConfig(); if (isset($config['templates']['template_404'])) { $view = $this->provider->getView(); $view->message = $message; $result = $view->renderFile($config['templates']['template_404']); } else { $body = sprintf("<h1>An error occurred</h1><h2>%s</h2>", $message); $result = sprintf("<html><head><title>%s</title><style>body {font-family: Helvetica,Arial,sans-serif;font-size: 20px;line-height: 28px;padding:20px;}</style></head><body>%s</body></html>", 'Tlumx application: ' . $message, $body); } $response->getBody()->write($result); $response->withHeader('Content-Type', 'text/html'); return $response; }
/** * @runInSeparateProcess */ public function testHandleTemplate() { $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'error.phtml'; $provider = new ServiceProvider(); $provider->setConfig(array('templates' => array('template_error' => $file))); $handler = new DefaultExceptionHandler($provider); $e = new \Exception(); $response = $handler->handle($e); $view = $provider->getView(); $view->message = 'Internal Server Error'; $result = $view->renderFile($file); $this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response); $body = $response->getBody(); $body->rewind(); $this->assertEquals($result, $body->getContents()); }