Skip to content

Commit e399a98

Browse files
committed
update exception handler
1 parent 4192a27 commit e399a98

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

webservice/app/Exceptions/Handler.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Auth\AuthenticationException;
7+
use Symfony\Component\HttpKernel\Exception\HttpException;
78
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
89

910
class Handler extends ExceptionHandler
@@ -44,9 +45,27 @@ public function report(Exception $exception)
4445
*/
4546
public function render($request, Exception $exception)
4647
{
48+
if ($request->expectsJson() && $exception instanceof HttpException) {
49+
return $this->handleExceptionJsonResponse($exception);
50+
}
51+
4752
return parent::render($request, $exception);
4853
}
4954

55+
/**
56+
* Handle the JSON response for the HTTP exceptions.
57+
*
58+
* @param \Exception $exception
59+
*
60+
* @return \Illuminate\Http\Response
61+
*/
62+
protected function handleExceptionJsonResponse(Exception $exception)
63+
{
64+
return response()->json([
65+
'messages' => [$exception->getMessage()],
66+
], $exception->getStatusCode());
67+
}
68+
5069
/**
5170
* Convert an authentication exception into an unauthenticated response.
5271
*

webservice/composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)