1212use Tempest \Http \HttpRequestFailed ;
1313use Tempest \Http \Request ;
1414use Tempest \Http \Response ;
15- use Tempest \Http \Responses \Json ;
1615use Tempest \Http \Session \CsrfTokenDidNotMatch ;
1716use Tempest \Http \Status ;
1817use Tempest \Router \ResponseSender ;
@@ -31,10 +30,16 @@ public function __construct(
3130 private ResponseSender $ responseSender ,
3231 private Container $ container ,
3332 private ExceptionReporter $ exceptionReporter ,
33+ private JsonHttpExceptionHandler $ jsonHandler ,
3434 ) {}
3535
3636 public function handle (Throwable $ throwable ): void
3737 {
38+ if (get (Request::class)->headers ->get ('Accept ' ) === 'application/json ' ) {
39+ $ this ->jsonHandler ->handle ($ throwable );
40+ return ;
41+ }
42+
3843 try {
3944 $ this ->exceptionReporter ->report ($ throwable );
4045
@@ -54,30 +59,20 @@ public function handle(Throwable $throwable): void
5459
5560 private function renderErrorResponse (Status $ status , ?HttpRequestFailed $ exception = null ): Response
5661 {
57- if (get (Request::class)->headers ->get ('Accept ' ) === 'application/json ' ) {
58- return new Json (
59- $ this ->appConfig ->environment ->isLocal () && $ exception !== null
60- ? [
61- 'message ' => $ exception ->getMessage (),
62- 'exception ' => get_class ($ exception ),
63- 'file ' => $ exception ->getFile (),
64- 'line ' => $ exception ->getLine (),
65- 'trace ' => arr ($ exception ->getTrace ())->map (
66- fn ($ trace ) => arr ($ trace )->removeKeys ('args ' )->toArray (),
67- )->toArray (),
68- ] : [
69- 'message ' => static ::getErrorMessage ($ status , $ exception ),
70- ],
71- )->setStatus ($ status );
72- }
73-
7462 return new GenericResponse (
7563 status: $ status ,
7664 body: new GenericView (__DIR__ . '/HttpErrorResponse/error.view.php ' , [
7765 'css ' => $ this ->getStyleSheet (),
7866 'status ' => $ status ->value ,
7967 'title ' => $ status ->description (),
80- 'message ' => static ::getErrorMessage ($ status , $ exception ),
68+ 'message ' => $ exception ?->getMessage() ?: match ($ status ) {
69+ Status::INTERNAL_SERVER_ERROR => 'An unexpected server error occurred ' ,
70+ Status::NOT_FOUND => 'This page could not be found on the server ' ,
71+ Status::FORBIDDEN => 'You do not have permission to access this page ' ,
72+ Status::UNAUTHORIZED => 'You must be authenticated in to access this page ' ,
73+ Status::UNPROCESSABLE_CONTENT => 'The request could not be processed due to invalid data ' ,
74+ default => null ,
75+ },
8176 ]),
8277 );
8378 }
@@ -86,18 +81,4 @@ private function getStyleSheet(): string
8681 {
8782 return Filesystem \read_file (__DIR__ . '/HttpErrorResponse/style.css ' );
8883 }
89-
90- private static function getErrorMessage (Status $ status , ?Throwable $ exception = null ): ?string
91- {
92- return (
93- $ exception ?->getMessage() ?: match ($ status ) {
94- Status::INTERNAL_SERVER_ERROR => 'An unexpected server error occurred ' ,
95- Status::NOT_FOUND => 'This page could not be found on the server ' ,
96- Status::FORBIDDEN => 'You do not have permission to access this page ' ,
97- Status::UNAUTHORIZED => 'You must be authenticated in to access this page ' ,
98- Status::UNPROCESSABLE_CONTENT => 'The request could not be processed due to invalid data ' ,
99- default => null ,
100- }
101- );
102- }
10384}
0 commit comments