| @@ 22-65 (lines=44) @@ | ||
| 19 | * It outputs the error message and diagnostic information in either JSON, XML, |
|
| 20 | * or HTML based on the Accept header. |
|
| 21 | */ |
|
| 22 | class Error extends AbstractError |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * Invoke error handler |
|
| 26 | * |
|
| 27 | * @param ServerRequestInterface $request The most recent Request object |
|
| 28 | * @param ResponseInterface $response The most recent Response object |
|
| 29 | * @param \Exception $exception The caught Exception object |
|
| 30 | * |
|
| 31 | * @return ResponseInterface |
|
| 32 | * @throws UnexpectedValueException |
|
| 33 | */ |
|
| 34 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Exception $exception) |
|
| 35 | { |
|
| 36 | $contentType = $this->determineContentType($request); |
|
| 37 | switch ($contentType) { |
|
| 38 | case 'application/json': |
|
| 39 | $output = Render::make('JsonErrorMessage', $exception, $this->displayErrorDetails); |
|
| 40 | break; |
|
| 41 | ||
| 42 | case 'text/xml': |
|
| 43 | case 'application/xml': |
|
| 44 | $output = Render::make('XmlErrorMessage', $exception, $this->displayErrorDetails); |
|
| 45 | break; |
|
| 46 | ||
| 47 | case 'text/html': |
|
| 48 | $output = Render::make('HtmlErrorMessage', $exception, $this->displayErrorDetails); |
|
| 49 | break; |
|
| 50 | ||
| 51 | default: |
|
| 52 | throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType); |
|
| 53 | } |
|
| 54 | ||
| 55 | $this->writeToErrorLog($exception); |
|
| 56 | ||
| 57 | $body = new Body(fopen('php://temp', 'r+')); |
|
| 58 | $body->write($output); |
|
| 59 | ||
| 60 | return $response |
|
| 61 | ->withStatus(500) |
|
| 62 | ->withHeader('Content-type', $contentType) |
|
| 63 | ->withBody($body); |
|
| 64 | } |
|
| 65 | } |
|
| 66 | ||
| @@ 22-64 (lines=43) @@ | ||
| 19 | * It outputs the error message and diagnostic information in either JSON, XML, |
|
| 20 | * or HTML based on the Accept header. |
|
| 21 | */ |
|
| 22 | class PhpError extends AbstractError |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * Invoke error handler |
|
| 26 | * |
|
| 27 | * @param ServerRequestInterface $request The most recent Request object |
|
| 28 | * @param ResponseInterface $response The most recent Response object |
|
| 29 | * @param \Throwable $error The caught Throwable object |
|
| 30 | * |
|
| 31 | * @return ResponseInterface |
|
| 32 | * @throws UnexpectedValueException |
|
| 33 | */ |
|
| 34 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Throwable $error) |
|
| 35 | { |
|
| 36 | $contentType = $this->determineContentType($request); |
|
| 37 | switch ($contentType) { |
|
| 38 | case 'application/json': |
|
| 39 | $output = Render::make('JsonPhpErrorMessage', $error, $this->displayErrorDetails); |
|
| 40 | break; |
|
| 41 | ||
| 42 | case 'text/xml': |
|
| 43 | case 'application/xml': |
|
| 44 | $output = Render::make('XmlPhpErrorMessage', $error, $this->displayErrorDetails); |
|
| 45 | break; |
|
| 46 | ||
| 47 | case 'text/html': |
|
| 48 | $output = Render::make('HtmlPhpErrorMessage', $error, $this->displayErrorDetails); |
|
| 49 | break; |
|
| 50 | default: |
|
| 51 | throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType); |
|
| 52 | } |
|
| 53 | ||
| 54 | $this->writeToErrorLog($error); |
|
| 55 | ||
| 56 | $body = new Body(fopen('php://temp', 'r+')); |
|
| 57 | $body->write($output); |
|
| 58 | ||
| 59 | return $response |
|
| 60 | ->withStatus(500) |
|
| 61 | ->withHeader('Content-type', $contentType) |
|
| 62 | ->withBody($body); |
|
| 63 | } |
|
| 64 | } |
|
| 65 | ||