Skip to content

Commit

Permalink
feat: update error class
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed May 31, 2024
1 parent 84e00b9 commit 402ebb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
23 changes: 9 additions & 14 deletions src/Core/Facades/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ private function process(array $route): mixed
}

$result = $this->app->make(Middleware::class, [$middlewares])
->handle(
$this->request,
$this->coreMiddleware($controller, $function)
);
->handle($this->request, $this->coreMiddleware($controller, $function));

$error = error_get_last();
if ($error !== null) {
Expand Down Expand Up @@ -177,10 +174,10 @@ private function runRoute(): mixed
protected function handleHttpException(HttpException $th): int
{
try {
$this->respond->send($this->respond->transform($th->__toString()));
$this->respond->send($th->__toString());
} catch (Throwable $th) {
$this->respond->clean();
$this->respond->send($this->respond->transform($this->handleError($th)));
$this->respond->send($this->handleError($th));
} finally {
return 1;
}
Expand All @@ -195,16 +192,14 @@ protected function handleHttpException(HttpException $th): int
protected function handleError(Throwable $th): mixed
{
try {
$kernel = $this->kernel->error();
$kernelError = new $kernel($th);
$kernelError->report();
$kernelError = $this->app->make($this->kernel->error());
$kernelError->setThrowable($th)->report();

// Force close stream.
$kernelError->__destruct();
return $kernelError->render($th);
} catch (Throwable $th) {
$error = new Error($th);
return $error->report()->render($th);
return $kernelError->render();
} catch (Throwable $t) {
return $this->app->make(Error::class)->setThrowable($t)->report()->render();
}
}

Expand Down Expand Up @@ -238,7 +233,7 @@ public function run(): int
return $this->handleHttpException($th);
}

$this->respond->send($this->respond->transform($this->handleError($th)));
$this->respond->send($this->handleError($th));
return 1;
}
}
Expand Down
21 changes: 15 additions & 6 deletions src/Core/Support/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ class Error
/**
* Init object.
*
* @param Throwable $throwable
* @return void
*/
public function __construct(Throwable $throwable)
public function __construct()
{
$this->throwable = $throwable;
$this->stream = fopen('php://stderr', 'wb');
}

Expand Down Expand Up @@ -119,6 +117,18 @@ protected function view(string $path, array $data = []): View
return $view;
}

/**
* Set Throwable.
*
* @param Throwable $t
* @return Error
*/
public function setThrowable(Throwable $t): Error
{
$this->throwable = $t;
return $this;
}

/**
* Get Throwable.
*
Expand Down Expand Up @@ -196,10 +206,9 @@ public function report(): Error
/**
* Show error to dev.
*
* @param Throwable $th
* @return mixed
*/
public function render(Throwable $th): mixed
public function render(): mixed
{
if (!debug()) {
return unavailable();
Expand All @@ -209,7 +218,7 @@ public function render(Throwable $th): mixed
respond()->setCode(Respond::HTTP_INTERNAL_SERVER_ERROR);

if (!request()->ajax()) {
return render(helper_path('/errors/trace'), ['error' => $th]);
return render(helper_path('/errors/trace'), ['error' => $this->throwable]);
}

respond()->getHeader()->set('Content-Type', 'application/json');
Expand Down

0 comments on commit 402ebb9

Please sign in to comment.