summaryrefslogtreecommitdiff
path: root/src/lib/error.php
blob: ae772d242f1fd165b790458d4dde80d13b628e32 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/// CRIMSON --- A simple PHP framework.
/// Copyright © 2024 Freya Murphy <contact@freyacat.org>
///
/// This file is part of CRIMSON.
///
/// CRIMSON is free software; you can redistribute it and/or modify it
/// under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 3 of the License, or (at
/// your option) any later version.
///
/// CRIMSON is distributed in the hope that it will be useful, but
/// WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with CRIMSON. If not, see <http://www.gnu.org/licenses/>.

/**
 * CRIMSON ERROR FUNCTIONS
 *
 * Insead of using trigger_error, crimson has its own CRIMSON_<type> error
 * functions. It is prefered to use this they hook more nicely into crimson.
 * trigger_error is still okay to use since crimson creates its own error
 * handler, and will catch it anyways.
 *
 * WARNING: DO NOT create your own error handler since this MAY interfere with
 * crimson's control flow.
 */

// crimson's fatal error handler will set $errno to CRIMSON_E_FATAL_ERROR.
define('CRIMSON_E_FATAL_ERROR', 0);

function __CRIMSON_error_pretty_print_name(int $errno) {
	switch ($errno) {
	case CRIMSON_E_FATAL_ERROR: // 0 //
		return 'Fatal Error';

	case E_ERROR: // 1 //
	case E_CORE_ERROR: // 16 //
	case E_USER_ERROR: // 256 //
	case E_RECOVERABLE_ERROR: // 4096 //
		return 'Error';

	case E_WARNING: // 2 //
	case E_CORE_WARNING: // 32 //
	case E_USER_WARNING: // 512 //
		return 'Warning';

	case E_PARSE: // 4 //
		return 'Parse Error';

	case E_NOTICE: // 8 //
	case E_USER_NOTICE: // 1024 //
		return 'Notice';

	case E_COMPILE_ERROR: // 128 //
		return 'Compile Error';

	case E_DEPRECATED: // 8192 //
	case E_USER_DEPRECATED: // 16384 //
		return 'Deprecated';

	default:
		return 'Unknown Error';
	}
}

function __CRIMSON_error_pretty_print_bg_color(int $errno) {
	switch ($errno) {
	case CRIMSON_E_FATAL_ERROR: // 0 //
	case E_ERROR: // 1 //
	case E_PARSE: // 4 //
	case E_CORE_ERROR: // 16 //
	case E_COMPILE_ERROR: // 128 //
	case E_USER_ERROR: // 256 //
	case E_RECOVERABLE_ERROR: // 4096 //
		return '#dc143c';

	case E_WARNING: // 2 //
	case E_CORE_WARNING: // 32 //
	case E_USER_WARNING: // 512 //
	case E_DEPRECATED: // 8192 //
	case E_USER_DEPRECATED: // 16384 //
		return '#db6d13';

	case E_NOTICE: // 8 //
	case E_USER_NOTICE: // 1024 //
	default:
		return '#13a6db';
	}
}

function __CRIMSON_error_pretty_print_text_color(int $errno) {
	switch ($errno) {
	case CRIMSON_E_FATAL_ERROR: // 0 //
	case E_ERROR: // 1 //
	case E_PARSE: // 4 //
	case E_CORE_ERROR: // 16 //
	case E_COMPILE_ERROR: // 128 //
	case E_USER_ERROR: // 256 //
	case E_RECOVERABLE_ERROR: // 4096 //
		return '#fff';

	case E_WARNING: // 2 //
	case E_CORE_WARNING: // 32 //
	case E_USER_WARNING: // 512 //
	case E_DEPRECATED: // 8192 //
	case E_USER_DEPRECATED: // 16384 //
		return '#fff';

	case E_NOTICE: // 8 //
	case E_USER_NOTICE: // 1024 //
	default:
		return '#181818';
	}
}

/**
 * __CRIMSON_error_pretty_print
 *
 * Prints a pretty HTML error message using the same set of parameters
 * from PHP's error handler.
 *
 * Unless CRIMSON detects that you are using a tty, crimson will opt to
 * pretty print all errors.
 */
function __CRIMSON_error_pretty_print(
	 int    $errno,
	 string $errstr,
	?string $errfile = NULL,
	?int    $errline = NULL,
	?array  $errcontext = NULL,
): void {

	$name = __CRIMSON_error_pretty_print_name($errno);
	$bg   = __CRIMSON_error_pretty_print_bg_color($errno);
	$text = __CRIMSON_error_pretty_print_text_color($errno);

	$root_style = "
	all: unset !important;
	display: block !important;
	margin: .1em !important;
	background: {$bg} !important;
	color: {$text} !important;
	font-family: Helvetica, Verdana, Courier, monospace !important;
	font-size: 14px !important;";

	$div_style = "
	padding: .5em; !important;";

	$span_style = "
	display: block !important";

	$title_style="
	font-size: 1.2em !important;
	font-weight: bold !important;
	background: rgba(255,255,255,.2); !important";

	$html = <<<EOF
<div style="{$root_style}">
	<div style="{$div_style}\n{$title_style}">
		(!) {$name}
	</div>
	<div style="{$div_style}">
		<span style="{$span_style}">
			In file {$errfile}:{$errline}
		</span>
		<span style="{$span_style}">
			>>> {$errstr}
		</span>
	</div>
</div>
EOF;
	echo $html;
}

function __CRIMSON_error_print(
	 int    $errno,
	 string $errstr,
	?string $errfile = NULL,
	?int    $errline = NULL,
	?array  $errcontext = NULL,
): void {
	$name = __CRIMSON_error_pretty_print_name($errno);
	echo "{$name}: {$errstr}\n\tIn file {$errfile}:{$errline}\n";
}

function CRIMSON_error_handler(
	 int    $errno,
	 string $errstr,
	?string $errfile = NULL,
	?int    $errline = NULL,
	?array  $errcontext = NULL,
): void {
	__CRIMSON_error_pretty_print($errno, $errstr, $errfile, $errline, $errcontext);
}

set_error_handler("CRIMSON_error_handler");

function CRIMSON_ERROR(string $msg): void {
	$frame = debug_backtrace()[1];
	CRIMSON_error_handler(E_ERROR, $msg,
		$frame['file'], $frame['line']);
}

function CRIMSON_FATAL_ERROR(string $msg): void {
	$frame = debug_backtrace()[1];
	CRIMSON_error_handler(CRIMSON_E_FATAL_ERROR, $msg,
		$frame['file'], $frame['line']);
}

function CRIMSON_WARNING(string $msg): void {
	$frame = debug_backtrace()[1];
	CRIMSON_error_handler(E_WARNING, $msg,
		$frame['file'], $frame['line']);
}