Skip to content

Commit

Permalink
Added JSON error response in ErrorHandler Middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <tom@inflatablecookie.com>
  • Loading branch information
betterthanclay committed Dec 8, 2023
1 parent 760d613 commit f3b069f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* Added allowed headers to CORS Middleware
* Added JSON error response in ErrorHandler Middleware

## v0.2.12 (2023-12-08)
* Added CORS Middleware
Expand Down
11 changes: 11 additions & 0 deletions src/Middleware/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ protected function handleCatastrophe(
Throwable $f,
Request $request
): Response {
if ($request->getHeaderLine('Accept') === 'application/json') {
return Harvest::json([
'error' => (string)(
$f instanceof NotFoundException ?
$e : $f
),
], 500, [
'Access-Control-Allow-Origin' => '*'
]);
}

if (class_exists(Glitch::class)) {
Glitch::handleException(
$f instanceof NotFoundException ?
Expand Down

0 comments on commit f3b069f

Please sign in to comment.