summaryrefslogtreecommitdiff
path: root/src/lib/error.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/error.php')
-rw-r--r--src/lib/error.php218
1 files changed, 218 insertions, 0 deletions
diff --git a/src/lib/error.php b/src/lib/error.php
new file mode 100644
index 0000000..ae772d2
--- /dev/null
+++ b/src/lib/error.php
@@ -0,0 +1,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']);
+}