From 2ee655b9d11f44fce5bfac82c808d0807f518e4d Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 30 Dec 2017 09:03:45 -0600 Subject: [PATCH] show error messages in json for API calls #92 --- app/Exceptions/Handler.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 54e621d0..b5cc5676 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -50,6 +50,23 @@ class Handler extends ExceptionHandler return redirect()->guest('login'); } + if($request->is('api/*')) { + $status = 400; + $http_code = $exception->getCode(); + if ($this->isHttpException($exception)) { + $status = $exception->getStatusCode(); + $http_code = $exception->getStatusCode(); + } + + return response()->json([ + 'error' => [ + 'code' => $exception->getCode() , + 'http_code' => $http_code, + 'message' => $exception->getMessage() + ] + ], $status); + } + return parent::render($request, $exception); }