Skip to content

Commit

Permalink
Merge pull request #189
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
leonardosahon authored Oct 28, 2024
2 parents fcfa9f7 + be3f855 commit 33e152e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
52 changes: 31 additions & 21 deletions src/Core/CoreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = <<<DEBUG
<details style='padding-left: 5px; margin: 5px 0 10px'>
<summary style="margin-bottom: 10px"><span style="font-size: 20px; font-weight: bold; cursor: pointer;">X-INFO</span></summary>
Expand Down Expand Up @@ -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,
];
}

Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -471,6 +474,7 @@ class_alias(get_class($anon_class), $exception_class);
"json_packet" => $opt['json_packet'] ?? null,
]
);

else {
$act = $this->container(
null,
Expand All @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions src/Core/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -34,20 +36,20 @@ 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);
}

/**
* 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'];
}
Expand Down

0 comments on commit 33e152e

Please sign in to comment.