-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kernel exception handler; termination middleware; request+response cl…
…ass updates
- Loading branch information
Showing
9 changed files
with
519 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Magnetar\Http; | ||
|
||
use Throwable; | ||
|
||
use Magnetar\Application; | ||
use Magnetar\Http\Request; | ||
use Magnetar\Http\Response; | ||
use Magnetar\Router\RouteUnassignedException; | ||
|
||
class ExceptionHandler { | ||
protected Throwable $caughtException; | ||
|
||
public function __construct( | ||
/** | ||
* The application instance | ||
* @var Application | ||
*/ | ||
protected Application $app | ||
) { | ||
|
||
} | ||
|
||
/** | ||
* Record the exception | ||
* @param Throwable $e | ||
* @return void | ||
*/ | ||
public function record(Throwable $e): void { | ||
$this->caughtException = $e; | ||
} | ||
|
||
/** | ||
* Render the exception | ||
* @param Request $request | ||
* @param Throwable $e | ||
* @return Response | ||
*/ | ||
public function render(Request $request, Throwable $e): Response { | ||
$this->record($e); | ||
|
||
$theme_file = 'errors/503'; | ||
$response_status = 503; | ||
|
||
if($e instanceof RouteUnassignedException) { | ||
$theme_file = 'errors/404'; | ||
$response_status = 404; | ||
} | ||
|
||
$response = (new Response())->status($response_status)->setBody( | ||
$this->app->make('theme')->tpl($theme_file, [ | ||
'request' => $request, | ||
'message' => $e->getMessage(), | ||
'file' => $e->getFile(), | ||
'line' => $e->getLine(), | ||
'trace' => $e->getTraceAsString() | ||
]) | ||
); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.