-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternalErrorPage.php
More file actions
143 lines (129 loc) · 3.88 KB
/
Copy pathInternalErrorPage.php
File metadata and controls
143 lines (129 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare( strict_types = 1 );
namespace DanielWebsite\Pages;
use DanielEScherzer\HTMLBuilder\FluentHTML;
use Throwable;
class InternalErrorPage extends BasePage {
private Throwable $error;
public function __construct( Throwable $error ) {
parent::__construct();
$this->head->append(
FluentHTML::fromTag( 'title' )->addChild( 'Internal Error' )
);
$this->error = $error;
$this->setResponseCode( 500 );
}
protected function build(): void {
$this->addStyleSheet( 'error-styles.css' );
$this->head->append(
FluentHTML::make(
'style',
[],
[
'pre { overflow-x: auto; padding-bottom: 10px; }',
'.error-box { width: fit-content; max-width: 100%; }',
]
)
);
$error = $this->error;
$file = self::formatFile( $error->getFile() );
$this->contentWrapper->append(
FluentHTML::make(
'div',
[ 'class' => 'error-box' ],
[
FluentHTML::make( 'h1', [], 'Internal Error' ),
FluentHTML::make(
'p',
[],
'[' . get_class( $error ) . '] ' . $error->getMessage(),
),
FluentHTML::make(
'p',
[],
[
'From ',
FluentHTML::make( 'code', [], $file ),
' line ',
FluentHTML::make( 'code', [], (string)$error->getLine() ),
]
),
FluentHTML::make( 'p', [], 'Backtrace:' ),
FluentHTML::make(
'pre',
[],
self::formatTrace( $error )
),
]
)
);
}
public static function handleException( Throwable $error ) {
try {
$page = new InternalErrorPage( $error );
$page->getResponse()->applyResponse();
} catch ( Throwable $error2 ) {
self::handleManually( $error, $error2 );
}
}
public static function handleManually( Throwable $error1, Throwable $error2 ) {
http_response_code( 500 );
echo "<!DOCTYPE html>\n";
echo "<html lang='en'>\n";
echo "<head>\n";
echo "<title>Internal Error</title>\n";
echo "<style> .content-wrapper { margin: auto; width: 80%; } </style>\n";
echo "</head>\n<body>\n";
echo "<div class='content-wrapper'>\n";
echo "<h1>Internal Error</h1>\n";
echo '<p>[' . get_class( $error1 ) . '] ' . $error1->getMessage() . "</p>\n";
$file = self::formatFile( $error1->getFile() );
echo "<p>From: <code>$file</code> line " . $error1->getLine() . "</p>\n";
echo "<p>Backtrace:</p>\n";
echo "<pre>" . self::formatTrace( $error1 ) . "</pre>\n";
echo "<p><b>While trying to handle that error, the handler also had an error:</b></p>\n";
echo '<p>[' . get_class( $error2 ) . '] ' . $error2->getMessage() . "</p>\n";
$file = self::formatFile( $error2->getFile() );
echo "<p>From: <code>$file</code> line " . $error2->getLine() . "</p>\n";
echo "<p>Backtrace:</p>\n";
echo "<pre>" . self::formatTrace( $error2 ) . "</pre>\n";
echo "</div></body></html>";
}
/**
* Like Exception::getTraceAsString() but
* - the `/var/www/html/` is removed from the start of files
*/
private static function formatTrace( Throwable $error ): string {
// Don't want to try and reimplement the entirety of the exception
// formatting
$trace = $error->getTraceAsString();
$lines = explode( "\n", $trace );
$betterLines = array_map(
static fn ( $line ) => preg_replace_callback(
"/^(#\d+) ([^\(]+)(\(\d+\): .*$)/",
static fn ( array $matches ): string => $matches[1]
. ' '
. self::formatFile( $matches[2] )
. $matches[3],
$line
),
$lines
);
return implode( "\n", $betterLines );
}
/**
* Nicely format file names, removing common leading prefixes
*/
private static function formatFile( string $file ): string {
// Within docker container
if ( str_starts_with( $file, '/var/www/html/' ) ) {
return '.../' . substr( $file, strlen( '/var/www/html/' ) );
}
// Within GitHub actions
$ghPrefix = '/home/runner/work/website-content/website-content/';
if ( str_starts_with( $file, $ghPrefix ) ) {
return '.../' . substr( $file, strlen( $ghPrefix ) );
}
return $file;
}
}