diff --git a/src/Core/CoreException.php b/src/Core/CoreException.php index c8ead5f..18f6e0d 100644 --- a/src/Core/CoreException.php +++ b/src/Core/CoreException.php @@ -10,6 +10,8 @@ use BrickLayer\Lay\Core\Enums\LayMode; use BrickLayer\Lay\Core\Traits\IsSingleton; use BrickLayer\Lay\Libs\LayDir; +use BrickLayer\Lay\Libs\LayFn; +use Throwable; class CoreException { @@ -76,19 +78,19 @@ public function get_env(): string * @throws \Exception */ public function use_exception( - string $title, - string $body, - bool $kill = true, - array $trace = [], - array $raw = [], - bool $use_lay_error = true, - array $opts = [], - ?\Throwable $exception = null, - bool $throw_500 = true, - bool $error_as_json = true, - ?array $json_packet = null, - bool $return_as_string = false, - bool $ascii = true, + string $title, + string $body, + bool $kill = true, + array $trace = [], + array $raw = [], + bool $use_lay_error = true, + array $opts = [], + ?Throwable $exception = null, + bool $throw_500 = true, + bool $error_as_json = true, + ?array $json_packet = null, + bool $return_as_string = false, + bool $ascii = true, ): ?array { if($exception) { @@ -192,7 +194,7 @@ private function container(?string $title, ?string $body, array $other = []): ar $env = $this->get_env(); $return_as_string = $other['as_string'] ?: false; - $display = $env == "DEVELOPMENT" || $other['core'] == "view"; + $display_error = $env == "DEVELOPMENT" || $other['core'] == "view"; $cli_mode = LayConfig::get_mode() === LayMode::CLI; $use_json = $cli_mode ? false : ($this->throw_as_json && !isset(LayConfig::user_agent()['browser'])); $show_internal_trace = $other['show_internal_trace'] ?? self::$show_internal_trace; @@ -352,7 +354,7 @@ private function container(?string $title, ?string $body, array $other = []): ar ]; } - if (!$use_json && !$cli_mode && $display) { + if (!$use_json && !$cli_mode && $display_error) { $ERROR_BODY = << X-INFO @@ -404,8 +406,9 @@ private function container(?string $title, ?string $body, array $other = []): ar return [ "act" => $other['act'] ?? "allow", - "error" => $error ?? ($display ?: ($write ? "Check logs for details. Error encountered" : "Error encountered, but could not write to log file due to insufficient permission!")), - "as_string" => $return_as_string + "error" => $error ?? (@$display ?: ($write ? "Check logs for details. Error encountered" : "Error encountered, but could not write to log file due to insufficient permission!")), + "as_string" => $return_as_string, + "display_error" => $display_error, ]; } @@ -433,8 +436,8 @@ private function show_exception($opt = []): ?array if(self::$already_caught) return null; - if(@LayConfig::get_mode() === LayMode::HTTP && $this->throw_500) - header("HTTP/1.1 500 Internal Server Error"); + if(LayConfig::get_mode() === LayMode::HTTP && $this->throw_500) + LayFn::header("HTTP/1.1 500 Internal Server Error"); $use_lay_error = $opt['use_lay_error'] ?? true; $type = $opt['exception_type']; @@ -471,6 +474,7 @@ class_alias(get_class($anon_class), $exception_class); "json_packet" => $opt['json_packet'] ?? null, ] ); + else { $act = $this->container( null, @@ -488,10 +492,16 @@ class_alias(get_class($anon_class), $exception_class); if($act['as_string']) return $act; - if ($act['act'] == "kill") { + if($act['display_error']) { + if(LayConfig::get_mode() === LayMode::HTTP && $this->throw_500) + LayFn::header("HTTP/1.1 500 Internal Server Error"); + self::$already_caught = true; - error_reporting(0); echo $act['error']; + } + + if ($act['act'] == "kill") { + error_reporting(0); die; } diff --git a/src/Core/Exception.php b/src/Core/Exception.php index 50d2dd2..59d8e75 100644 --- a/src/Core/Exception.php +++ b/src/Core/Exception.php @@ -2,11 +2,13 @@ declare(strict_types=1); namespace BrickLayer\Lay\Core; +use Throwable; + abstract class Exception { /** * @throws \Exception */ - public static function throw_exception(string $message, string $title = "Generic", bool $kill = true, bool $use_lay_error = true, array $stack_track = [], ?\Throwable $exception = null, bool $throw_500 = true, bool $error_as_json = true, ?array $json = null, bool $as_string = false, bool $ascii = true) : ?array + public static function throw_exception(string $message, string $title = "Generic", bool $kill = true, bool $use_lay_error = true, array $stack_track = [], ?Throwable $exception = null, bool $throw_500 = true, bool $error_as_json = true, ?array $json = null, bool $as_string = false, bool $ascii = true) : ?array { return self::new()->use_exception($title, $message, $kill, trace: $stack_track, use_lay_error: $use_lay_error, exception: $exception, throw_500: $throw_500, error_as_json: $error_as_json, json_packet: $json, return_as_string: $as_string, ascii: $ascii); } @@ -34,7 +36,7 @@ public static function always_log() : void self::new()->log_always(); } - public static function log(mixed $message, $exception = null) : void + public static function log(mixed $message, Throwable $exception = null) : void { self::always_log(); self::throw_exception(var_export($message, true), "ManualLog", kill: false, exception: $exception, throw_500: false); @@ -42,12 +44,12 @@ public static function log(mixed $message, $exception = null) : void /** * Get the error message the Lay way - * @param \Throwable $exception + * @param Throwable $exception * @param bool $add_ascii_char * @return string * @throws \Exception */ - public static function text(\Throwable $exception, bool $add_ascii_char = true) : string + public static function text(Throwable $exception, bool $add_ascii_char = true) : string { return self::throw_exception("", "Text Extraction", exception: $exception, as_string: true, ascii: $add_ascii_char)['error']; }