Skip to content

Commit

Permalink
Add support of web validation errors
Browse files Browse the repository at this point in the history
If this package is used in an application that uses api AND common web parts together, package should support native Laravel form validation flow like `response()->withErrors([])`. 

This change will handle this behaviour. You can just throw validation exception, and everything will work.
  • Loading branch information
mlanin authored Dec 24, 2016
1 parent 61d4dc7 commit 0032370
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/ExceptionHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function render($request, Exception $e)
{
$e = $this->resolveException($e);

$response = $request->expectsJson() || !function_exists('view')
? $this->renderForApi($e)
: $this->renderHtmlPage($e);
$response = $request->expectsJson() || ! function_exists('view')
? $this->renderForApi($e, $request)
: $this->renderForHtml($e, $request);

return $response->withException($e);
}
Expand All @@ -47,9 +47,10 @@ public function render($request, Exception $e)
* Render exceptions for json API.
*
* @param ApiException $e
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
protected function renderForApi(ApiException $e)
protected function renderForApi(ApiException $e, $request)
{
return response()->json($e, $e->getCode(), $e->getHeaders());
}
Expand All @@ -58,17 +59,32 @@ protected function renderForApi(ApiException $e)
* Render exception for common html request.
*
* @param ApiException $e
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
*/
protected function renderHtmlPage(ApiException $e)
protected function renderForHtml(ApiException $e, $request)
{
$status = $e->getCode();

return view()->exists("errors.{$status}")
? response(view("errors.{$status}", ['exception' => $e]), $status, $e->getHeaders())
: (view()->exists("laravel-api-exceptions::errors.{$status}")
? response(view("laravel-api-exceptions::errors.{$status}", ['exception' => $e]), $status, $e->getHeaders())
: $this->renderForApi($e));
return $e instanceof ValidationFailedApiException
? $this->convertValidationApiExceptionToResponse($e, $request)
: (view()->exists("errors.{$status}")
? response(view("errors.{$status}", ['exception' => $e]), $status, $e->getHeaders())
: (view()->exists("laravel-api-exceptions::errors.{$status}")
? response(view("laravel-api-exceptions::errors.{$status}", ['exception' => $e]), $status, $e->getHeaders())
: $this->renderForApi($e, $request)));
}

/**
* Create a response object from the given validation exception.
*
* @param ValidationFailedApiException $e
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function convertValidationApiExceptionToResponse(ValidationFailedApiException $e, $request)
{
return redirect()->back()->withInput($request->input())->withErrors($e->getNativeErrors());
}

/**
Expand Down

0 comments on commit 0032370

Please sign in to comment.