Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
31 / 31 |
InternalServerErrorResponse | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
31 / 31 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
render | |
100.00% |
1 / 1 |
5 | |
100.00% |
27 / 27 |
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 | $exceptionMessage = ($this->getCode() || $this->getMessage() ? '<p>Actual exception: (' |
35 | . htmlspecialchars(get_class($this)) |
36 | . ') #' . htmlspecialchars($this->getCode()) . ': ' . htmlspecialchars($this->getMessage()) |
37 | . '<br>Previous exception:' . PHP_EOL : '<p>Exception:' |
38 | ) |
39 | |
40 | . ($this->getPrevious() ? ' (' . htmlspecialchars(get_class($this->getPrevious())) . ') #' |
41 | . htmlspecialchars($this->getPrevious()->getCode()) . ': ' |
42 | . htmlspecialchars($this->getPrevious()->getMessage()) . '</p>' . PHP_EOL : ''); |
43 | |
44 | // When JSON is requested, return JSON error |
45 | if ($request->getServer('HTTP_ACCEPT') == 'application/json') { |
46 | return json_encode([ |
47 | 'code' => 500, |
48 | 'error' => 'Internal Server Error', |
49 | 'message' => strip_tags($exceptionMessage), |
50 | ], JSON_PRETTY_PRINT); |
51 | } |
52 | |
53 | return '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . PHP_EOL |
54 | . '<html><head>' . PHP_EOL |
55 | . '<title>500 Internal Server Error</title>' . PHP_EOL |
56 | . '</head><body>' . PHP_EOL |
57 | . '<h1>Internal Server Error</h1>' . PHP_EOL |
58 | . '<p>The server encountered an internal error or misconfiguration and was unable to complete your ' |
59 | . 'request.</p><p>Please contact the server administrator <!-- at root@localhost --> to inform them of the ' |
60 | . 'time this error occured, and the actions you performed just before this error.</p>' |
61 | |
62 | . $exceptionMessage |
63 | |
64 | . '<p>More information about this error may be available in the server log.</p>' . PHP_EOL |
65 | . '<hr>' . PHP_EOL |
66 | . ($request->getServer('SERVER_SIGNATURE') ?? '') . PHP_EOL |
67 | . '</body></html>'; |
68 | } |
69 | } |