Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
InternalServerErrorResponse | |
100.00% |
34 / 34 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
31 / 31 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | |
3 | namespace Miniframe\Response; |
4 | |
5 | use Miniframe\Core\Request; |
6 | use Miniframe\Core\Response; |
7 | |
8 | class InternalServerErrorResponse extends Response |
9 | { |
10 | /** |
11 | * Construct the exception. Note: The message is NOT binary safe. |
12 | * |
13 | * @param string $message The Exception message to throw. |
14 | * @param integer $code The Exception code. |
15 | * @param \Throwable|null $previous The previous throwable used for the exception chaining. |
16 | * |
17 | * @link https://php.net/manual/en/exception.construct.php |
18 | */ |
19 | public function __construct(string $message = '', int $code = 0, \Throwable $previous = null) |
20 | { |
21 | \Exception::__construct($message, $code, $previous); |
22 | $this->setResponseCode(500); |
23 | $this->setExitCode(1); |
24 | } |
25 | |
26 | /** |
27 | * Returns a basic HTML page explaining that something went wrong. |
28 | * |
29 | * @return string |
30 | */ |
31 | public function render(): string |
32 | { |
33 | $request = Request::getActual(); |
34 | $signature = $request->getServer('SERVER_SIGNATURE'); |
35 | $exceptionMessage = ($this->getCode() || $this->getMessage() ? '<p>Actual exception: (' |
36 | . htmlspecialchars(get_class($this)) |
37 | . ') #' . (string)$this->getCode() . ': ' . htmlspecialchars($this->getMessage()) |
38 | . '<br>Previous exception:' . PHP_EOL : '<p>Exception:' |
39 | ) |
40 | |
41 | . ($this->getPrevious() ? ' (' . htmlspecialchars(get_class($this->getPrevious())) . ') #' |
42 | . htmlspecialchars($this->getPrevious()->getCode()) . ': ' |
43 | . htmlspecialchars($this->getPrevious()->getMessage()) . '</p>' . PHP_EOL : ''); |
44 | |
45 | // When JSON is requested, return JSON error |
46 | if ($request->getServer('HTTP_ACCEPT') == 'application/json') { |
47 | $data = [ |
48 | 'code' => 500, |
49 | 'error' => 'Internal Server Error', |
50 | 'message' => strip_tags($exceptionMessage), |
51 | ]; |
52 | if ($signature) { |
53 | $data['signature'] = $signature; |
54 | } |
55 | return json_encode($data, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); |
56 | } |
57 | |
58 | return '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . PHP_EOL |
59 | . '<html><head>' . PHP_EOL |
60 | . '<title>500 Internal Server Error</title>' . PHP_EOL |
61 | . '</head><body>' . PHP_EOL |
62 | . '<h1>Internal Server Error</h1>' . PHP_EOL |
63 | . '<p>The server encountered an internal error or misconfiguration and was unable to complete your ' |
64 | . 'request.</p><p>Please contact the server administrator <!-- at root@localhost --> to inform them of the ' |
65 | . 'time this error occured, and the actions you performed just before this error.</p>' |
66 | |
67 | . $exceptionMessage |
68 | |
69 | . '<p>More information about this error may be available in the server log.</p>' . PHP_EOL |
70 | . '<hr>' . PHP_EOL |
71 | . (is_string($signature) ? $signature : '') . PHP_EOL |
72 | . '</body></html>'; |
73 | } |
74 | } |