8000 [Feature] Add request exception interface to exception parsing · rubinred/laravel-json-api@a360c7c · GitHub
[go: up one dir, main page]

Skip to content

Commit a360c7c

Browse files
committed
[Feature] Add request exception interface to exception parsing
Closes cloudcreativity#570
1 parent d2686e0 commit a360c7c

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. This projec
44

55
## Unreleased
66

7+
### Added
8+
- [#570](https://github.com/cloudcreativity/laravel-json-api/issues/570)
9+
Exception parser now handles the Symfony request exception interface.
10+
711
### Fixed
812
- Fixed qualifying column for morph-to-many relations. This was caused by Laravel introducing
913
a breaking change of adding a `qualifyColumn` method to the relation builder. Previously

src/Exceptions/ExceptionParser.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
3030
use Neomerx\JsonApi\Document\Error;
3131
use Neomerx\JsonApi\Exceptions\JsonApiException as NeomerxJsonApiException;
32+
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
3233
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
3334

3435
/**
@@ -101,6 +102,10 @@ protected function getErrors(\Throwable $e): array
101102
return [$this->getHttpError($e)];
102103
}
103104

105+
if ($e instanceof RequestExceptionInterface) {
106+
return [$this->getRequestError($e)];
107+
}
108+
104109
return [$this->getDefaultError()];
105110
}
106111

@@ -125,6 +130,22 @@ protected function getHttpError(HttpExceptionInterface $e): ErrorInterface
125130
return new Error(null, null, $status, null, $title, $e->getMessage() ?: null);
126131
}
127132

133+
/**
134+
* @param RequestExceptionInterface|\Throwable $e
135+
* @return ErrorInterface
136+
*/
137+
protected function getRequestError(RequestExceptionInterface $e): ErrorInterface
138+
{
139+
return new Error(
140+
null,
141+
null,
142+
$status = Response::HTTP_BAD_REQUEST,
143+
null,
144+
$this->getDefaultTitle($status),
145+
$e->getMessage() ?: null
146+
);
147+
}
148+
128149
/**
129150
* @return ErrorInterface
130151
*/

tests/lib/Integration/ErrorsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Illuminate\Validation\ValidationException;
3434
use Neomerx\JsonApi\Document\Error as NeomerxError;
3535
use Neomerx\JsonApi\Exceptions\JsonApiException as NeomerxException;
36+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
3637
use Symfony\Component\HttpKernel\Exception\HttpException;
3738

3839
class ErrorsTest extends TestCase
@@ -354,6 +355,25 @@ public function testTokenMismatch()
354355
->assertHeader('Content-Type', 'application/vnd.api+json');
355356
}
356357

358+
/**
359+
* The Symfony bad request exception does not implement the HTTP exception
360+
* interface, so we need to ensure we handle it.
361+
*/
362+
public function testBadRequestException(): void
363+
{
364+
$ex = new BadRequestException('The request format is bad.');
365+
366+
$expected = [
367+
'title' => 'Bad Request',
368+
'detail' => 'The request format is bad.',
369+
'status' => '400',
370+
];
371+
372+
$this->request($ex)
373+
->assertExactErrorStatus($expected)
374+
->assertHeader('Content-Type', 'application/vnd.api+json');
375+
}
376+
357377
/**
358378
* If we get a Laravel validation exception we need to convert this to
359379
* JSON API errors.

0 commit comments

Comments
 (0)
0