From da7376d461e09c7d2c7895cd8f868471d5cd1219 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 6 Aug 2015 12:07:04 -0500 Subject: [PATCH 1/2] working on exception handling for model not found. --- app/Exceptions/Handler.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 2cc435b7..e3c01023 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,7 +3,9 @@ namespace App\Exceptions; use Exception; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler @@ -15,6 +17,7 @@ class Handler extends ExceptionHandler */ protected $dontReport = [ HttpException::class, + ModelNotFoundException::class, ]; /** @@ -39,6 +42,10 @@ public function report(Exception $e) */ public function render($request, Exception $e) { + if ($e instanceof ModelNotFoundException) { + $e = new NotFoundHttpException(404, $e->getMessage(), $e); + } + return parent::render($request, $e); } } From 395b69b54020e5fba899613509e2affe4a2f04e0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 6 Aug 2015 12:07:35 -0500 Subject: [PATCH 2/2] fix status code --- app/Exceptions/Handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index e3c01023..3dabc68c 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -43,7 +43,7 @@ public function report(Exception $e) public function render($request, Exception $e) { if ($e instanceof ModelNotFoundException) { - $e = new NotFoundHttpException(404, $e->getMessage(), $e); + $e = new NotFoundHttpException($e->getMessage(), $e); } return parent::render($request, $e);