|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim Framework (https://slimframework.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/slimphp/Slim |
|
6
|
|
|
* @copyright Copyright (c) 2011-2017 Josh Lockhart |
|
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Slim\Handlers; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
use Slim\Http\Body; |
|
14
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Default Slim application error handler |
|
18
|
|
|
* |
|
19
|
|
|
* It outputs the error message and diagnostic information in either JSON, XML, |
|
20
|
|
|
* or HTML based on the Accept header. |
|
21
|
|
|
*/ |
|
22
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.