8000 Error handling updates. · josh-taylor/laravel-json-api@232be1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 232be1f

Browse files
committed
Error handling updates.
1 parent 94945f7 commit 232be1f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/Exceptions/HandlesErrors.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
namespace CloudCreativity\LaravelJsonApi\Exceptions;
2020

21+
use CloudCreativity\JsonApi\Document\Error;
2122
use CloudCreativity\LaravelJsonApi\Http\Responses\ResponseFactory;
2223
use CloudCreativity\LaravelJsonApi\Services\JsonApiService;
2324
use Exception;
2425
use Illuminate\Http\Request;
2526
use Illuminate\Http\Response;
2627
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
27-
use Neomerx\JsonApi\Document\Error;
2828
use Neomerx\JsonApi\Exceptions\JsonApiException;
2929
use Symfony\Component\HttpKernel\Exception\HttpException;
3030
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@@ -56,16 +56,24 @@ public function renderJsonApi(Request $request, Exception $e)
5656
{
5757
/** @var JsonApiService $service */
5858
$service = app(JsonApiService::class);
59-
$errors = $this->parseToErrors($e);
6059

6160
if (!$service->api()->hasEncoder()) {
62-
return $this->renderWithoutEncoder($errors);
61+
return $this->renderWithoutEncoder($e);
62+
}
63+
64+
$statusCode = null;
65+
66+
if ($e instanceof JsonApiException) {
67+
$statusCode = $e->getHttpCode();
68+
$errors = $e->getErrors()->getArrayCopy() ?: new Error(null, null, $statusCode);
69+
} else {
70+
$errors = $this->parseToErrors($e);
6371
}
6472

6573
/** @var ResponseFactory $responses */
6674
$responses = response()->jsonApi();
6775

68-
return $responses->errors($errors);
76+
return $responses->errors($errors, $statusCode);
6977
}
7078

7179
/**
@@ -74,11 +82,6 @@ public function renderJsonApi(Request $request, Exception $e)
7482
*/
7583
protected function parseToErrors(Exception $e)
7684
{
77-
if ($e instanceof JsonApiException) {
78-
$errs = $e->getErrors()->getArrayCopy();
79-
return $errs ?: new Error(null, null, $e->getHttpCode());
80-
}
81-
8285
$statusCode = ($e instanceof HttpExceptionInterface) ?
8386
$e->getStatusCode() :
8487
Response::HTTP_INTERNAL_SERVER_ERROR;
@@ -91,10 +94,10 @@ protected function parseToErrors(Exception $e)
9194
/**
9295
* Send a response if no JSON API encoder is available.
9396
*
94-
* @param $errors
97+
* @param Exception $e
9598
* @return Response
9699
*/
97-
protected function renderWithoutEncoder($errors)
100+
protected function renderWithoutEncoder(Exception $e)
98101
{
99102
return response('', Response::HTTP_NOT_ACCEPTABLE);
100103
}

src/Http/Requests/AbstractRequest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use CloudCreativity\JsonApi\Contracts\Store\StoreInterface;
2424
use CloudCreativity\JsonApi\Contracts\Validators\DocumentValidatorInterface;
2525
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorProviderInterface;
26+
use CloudCreativity\JsonApi\Document\Error;
2627
use CloudCreativity\JsonApi\Object\Document;
2728
use CloudCreativity\JsonApi\Object\ResourceIdentifier;
2829
use CloudCreativity\LaravelJsonApi\Contracts\Http\Requests\RequestHandlerInterface;
@@ -32,7 +33,6 @@
3233
use Illuminate\Http\Request as HttpRequest;
3334
use Illuminate\Http\Response;
3435
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
35-
use Neomerx\JsonApi\Document\Error;
3636
use Neomerx\JsonApi\Exceptions\JsonApiException;
3737
use RuntimeException;
3838
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -328,7 +328,8 @@ protected function validateParameters()
328328
$this->validators->filterResources() : null;
329329

330330
if ($validator && !$validator->isValid((array) $parameters->getFilteringParameters())) {
331-
throw new JsonApiException($validator->errors());
331+
$errors = $validator->errors();
332+
throw new JsonApiException($errors, Error::getErrorStatus($errors));
332333
}
333334

334335
return $parameters;
@@ -366,7 +367,8 @@ protected function validateDocument()
366367
$validator = $this->validator();
367368

368369
if ($validator && !$validator->isValid($this->getDocument())) {
369-
throw new JsonApiException($validator->errors());
370+
$errors = $validator->errors();
371+
throw new JsonApiException($errors, Error::getErrorStatus($errors));
370372
}
371373
}
372374

0 commit comments

Comments
 (0)
0