diff --git a/src/Bootstrap.php b/src/Bootstrap.php index eca37a1..044f25b 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -12,6 +12,7 @@ use IOcornerstone\Psr4AutoloaderClass; use IOcornerstone\Framework\{ Registry as Reg, Console, + Configure, CliDefaults, ErrorHandler, APICacheAge, @@ -62,6 +63,8 @@ $myErrorHandler->register(); Reg::set('loader', $loader); Reg::set('di', new DI()); // Initialize our Dependency Injector Reg::set('container', new AutowireContainer()); +Reg::set('error_handler', $myErrorHandler); +Reg::set('debug', $debug); Console::setupConsoleVars(); // Copy CLI Args into $_GET @@ -73,9 +76,16 @@ function isLive(): bool { if (Configure::has('IOcornerstone', 'live')) { $live = Configure::get('IOcornerstone', 'live'); - } - if ($live === null) { - $live = true; + + if ($live === null) { + $live = true; + } + + $debugger = ($live) ? false : true; + Reg::get('error_handler')->resetDebugger($debugger); + } else { + echo "Warning: LIVE not Set in Config!!!"; + $live = Reg::get('debug') ? false : true; } return (bool) $live; } diff --git a/src/Framework/ErrorHandler.php b/src/Framework/ErrorHandler.php index ca7b2b9..c608f3c 100644 --- a/src/Framework/ErrorHandler.php +++ b/src/Framework/ErrorHandler.php @@ -11,14 +11,26 @@ use Throwable; final class ErrorHandler { private bool $hasError = false; + private string $myErr = "An internal error occurred. Please try again later."; public function __construct( private bool $debug = false, ) { define('WORD_WRAP_CHRS', 80); // Letters before Line Wrap on Errors } + + public function setMyErr(string $em): void + { + $this->myErr = $em; + } + + public function resetDebugger(bool $debug) + { + $this->debug = $debug; + $this->setErrors(); + } - public function register(): void + private function setErrors(): void { if ($this->debug) { error_reporting(E_ALL); @@ -37,7 +49,11 @@ final class ErrorHandler // } error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED); } + } + public function register(): void + { + $this->setErrors(); set_error_handler([$this, 'handleError']); set_exception_handler([$this, 'handleException']); register_shutdown_function([$this, 'handleShutdown']); @@ -309,6 +325,22 @@ final class ErrorHandler return $msg; } + public function renderProdMessage(): string { + if (Console::isConsole()) { + return $this->myErr; + } + if ($this->isJsonRequest()) { + $this->setJsonHeaders(); + + return json_encode([ + 'error' => [ + 'message' => 'Internal Server Error' + ] + ]); + } + return "