From 3310063b59502585162b4cdcd05b13c31701e93c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 6 Jul 2017 11:49:51 -0500 Subject: [PATCH] add validation to exception handler --- app/Exceptions/Handler.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index e9f820eb..8a1ceb35 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -4,6 +4,7 @@ use Exception; use Illuminate\Auth\AuthenticationException; +use Illuminate\Validation\ValidationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler @@ -43,7 +44,25 @@ public function render($request, Exception $exception) } /** - * Convert an authentication exception into an unauthenticated response. + * Convert a validation exception into a response. + * + * @param \Illuminate\Http\Request $request + * @param Illuminate\Validation\ValidationException $exception + * @return \Illuminate\Http\Response + */ + protected function invalid($request, ValidationException $exception) + { + $errors = $exception->validator->errors()->messages(); + + return $request->expectsJson() + ? response()->json(['message' => $exception->getMessage(), 'errors' => $errors]) + : redirect()->back()->withInput()->withErrors( + $errors, $exception->errorBag + ); + } + + /** + * Convert an authentication exception into a response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Auth\AuthenticationException $exception