/** * Return an error into an HTTP or JSON data array. * * @param string $title * @return array */ public function error($title = null) { if ($title == null) { $title = $this->debug ? 'The application could not run because of the following error:' : 'A website error has occurred. Sorry for the temporary inconvenience.'; } $type = $this->request->getHeader('Content-Type'); $mesg = $this->exception->getMessage(); $file = $this->exception->getFile(); $line = $this->exception->getLine(); $code = $this->exception->getCode(); $statusCode = method_exists($this->exception, 'getStatusCode') ? $this->exception->getStatusCode() : null; // Check status code is null if ($statusCode == null) { $statusCode = $code >= 100 && $code <= 500 ? $code : 400; } $this->response->withStatus($statusCode); // Check logger exist if ($this->logger !== null) { // Send error to log $this->logger->addError($this->exception->getMessage()); } $this->isJson = isset($type[0]) && $type[0] == 'application/json'; // Check content-type is application/json if ($this->isJson) { // Define content-type to json $this->response->withHeader('Content-Type', 'application/json'); $error = ['status' => 'error', 'status_code' => $statusCode, 'error' => $title, 'details' => []]; // Check debug if ($this->debug) { $error['details'] = ['message' => $mesg, 'file' => $file, 'line' => $line, 'code' => $code]; } return $error; } // Define content-type to html $this->response->withHeader('Content-Type', 'text/html'); $message = sprintf('<span>%s</span>', htmlentities($mesg)); $error = ['type' => get_class($this->exception), $error['status_code'] = $statusCode, 'message' => $message]; // Check debug if ($this->debug) { $trace = $this->exception->getTraceAsString(); $trace = sprintf('<pre>%s</pre>', htmlentities($trace)); $error['file'] = $file; $error['line'] = $line; $error['code'] = $code; $error['trace'] = $trace; } $error['debug'] = $this->debug; $error['title'] = $title; return $error; }
/** * @param Throwable $exception */ public function handle(\Throwable $exception) { if ($exception instanceof HttpException) { http_response_code($exception->getStatusCode()); } if (false === $this->debug) { return; } if (null !== $exception->getPrevious()) { $exception = $exception->getPrevious(); } $message = $exception->getMessage(); $trace = $exception->getTrace(); array_unshift($trace, array('function' => '', 'file' => $exception->getFile() != null ? $exception->getFile() : null, 'line' => $exception->getLine() != null ? $exception->getLine() : null, 'args' => array())); $firstTrace = array_shift($trace); $firstTrace['excerpt'] = $this->excerpt($firstTrace['file'], $firstTrace['line']); array_unshift($trace, $firstTrace); echo $this->templateEngine->render(__DIR__ . '/templates/exception.php', array('message' => $message, 'trace' => $trace)); }
/** * Prepare a response in case an error occurred. * * @param \Throwable $exception * @param Http\Response $response * @return void */ protected function prepareErrorResponse($exception, Http\Response $response) { $pathPosition = strpos($exception->getFile(), 'Packages/'); $filePathAndName = $pathPosition !== false ? substr($exception->getFile(), $pathPosition) : $exception->getFile(); $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : ''; $content = PHP_EOL . 'Uncaught Exception in Flow ' . $exceptionCodeNumber . $exception->getMessage() . PHP_EOL; $content .= 'thrown in file ' . $filePathAndName . PHP_EOL; $content .= 'in line ' . $exception->getLine() . PHP_EOL . PHP_EOL; $content .= Debugger::getBacktraceCode($exception->getTrace(), false, true) . PHP_EOL; if ($exception instanceof Exception) { $statusCode = $exception->getStatusCode(); } else { $statusCode = 500; } $response->setStatus($statusCode); $response->setContent($content); $response->setHeader('X-Flow-ExceptionCode', $exception->getCode()); $response->setHeader('X-Flow-ExceptionMessage', $exception->getMessage()); }
/** * Display the given exception to the user. * * @param \Exception $exception * @return \Symfony\Component\HttpFoundation\Response */ public function display(\Throwable $exception) { $status = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; $headers = $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : array(); return new Response(file_get_contents(__DIR__ . '/resources/plain.html'), $status, $headers); }
/** * Display the given exception to the user. * * @param \Exception $exception * @return \Symfony\Component\HttpFoundation\Response */ public function display(\Throwable $exception) { $status = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; $headers = $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : array(); return new Response($this->whoops->handleException($exception), $status, $headers); }
/** * @param \Exception * * @return Int */ function getStatusCode(\Throwable $e) { return $e instanceof HttpExceptionInterface ? $e->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR; }