| @@ 34-64 (lines=31) @@ | ||
| 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 = $this->renderJsonErrorMessage($exception); |
|
| 40 | break; |
|
| 41 | ||
| 42 | case 'text/xml': |
|
| 43 | case 'application/xml': |
|
| 44 | $output = $this->renderXmlErrorMessage($exception); |
|
| 45 | break; |
|
| 46 | ||
| 47 | case 'text/html': |
|
| 48 | $output = $this->renderHtmlErrorMessage($exception); |
|
| 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 | /** |
|
| 67 | * Render HTML error page |
|
| @@ 34-63 (lines=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 = $this->renderJsonErrorMessage($error); |
|
| 40 | break; |
|
| 41 | ||
| 42 | case 'text/xml': |
|
| 43 | case 'application/xml': |
|
| 44 | $output = $this->renderXmlErrorMessage($error); |
|
| 45 | break; |
|
| 46 | ||
| 47 | case 'text/html': |
|
| 48 | $output = $this->renderHtmlErrorMessage($error); |
|
| 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 | /** |
|
| 66 | * Render HTML error page |
|