8000 [Feature] Move default 406 and 415 messages to exception classes · laravel-json-api/laravel@397cb2a · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 397cb2a

Browse files
committed
[Feature] Move default 406 and 415 messages to exception classes
1 parent 92cd7fc commit 397cb2a

File tree

5 files changed

+101
-13
lines changed

5 files changed

+101
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ All notable changes to this project will be documented in this file. This projec
1010
- The `JsonApiException` class now has a `context()` method. Laravel's exception handler uses this to add log context
1111
when the exception is logged. This means logging of JSON:API exceptions will now include the HTTP status code and the
1212
JSON:API errors.
13+
- Moved the default `406 Not Acceptable` and `415 Unsupported Media Type` messages to the following two new exception
14+
classes:
15+
- `Exceptions\HttpNotAcceptableException`
16+
- `Exceptions\HttpUnsupportedMediaTypeException`
1317

1418
### Fixed
1519

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* Copyright 2022 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\Laravel\Exceptions;
21+
22+
use Illuminate\Http\Response;
23+
use Symfony\Component\HttpKernel\Exception\HttpException;
24+
use Throwable;
25+
26+
class HttpNotAcceptableException extends HttpException
27+
{
28+
/**
29+
* HttpNotAcceptableException constructor.
30+
*
31+
* @param string|null $message
32+
* @param Throwable|null $previous
33+
* @param array $headers
34+
* @param int $code
35+
*/
36+
public function __construct(
37+
string $message = null,
38+
Throwable $previous = null,
39+
array $headers = [],
40+
int $code = 0
41+
) {
42+
if (null === $message) {
43+
$message = __("The requested resource is capable of generating only content not acceptable "
44+
. "according to the Accept headers sent in the request.");
45+
}
46+
47+
parent::__construct(Response::HTTP_NOT_ACCEPTABLE, $message, $previous, $headers, $code);
48+
}
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*
3+
* Copyright 2022 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\Laravel\Exceptions;
21+
22+
use Illuminate\Http\Response;
23+
use Symfony\Component\HttpKernel\Exception\HttpException;
24+
use Throwable;
25+
26+
class HttpUnsupportedMediaTypeException extends HttpException
27+
{
28+
/**
29+
* HttpUnsupportedMediaTypeException constructor.
30+
*
31+
* @param string|null $message
32+
* @param Throwable|null $previous
33+
* @param array $headers
34+
* @param int $code
35+
*/
36+
public function __construct(string $message = null, Throwable $previous = null, array $headers = [], int $code = 0)
37+
{
38+
if (null === $message) {
39+
$message = __('The request entity has a media type which the server or resource does not support.');
40+
}
41+
42+
parent::__construct(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, $message, $previous, $headers, $code);
43+
}
44+
}

src/Http/Requests/ResourceQuery.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121

2222
use Illuminate\Contracts\Validation\Validator;
2323
use Illuminate\Database\Eloquent\Model;
24-
use Illuminate\Http\Response;
2524
use LaravelJsonApi\Contracts\Auth\Authorizer;
2625
use LaravelJsonApi\Contracts\Query\QueryParameters;
2726
use LaravelJsonApi\Core\Exceptions\JsonApiException;
2827
use LaravelJsonApi\Core\Query\FieldSets;
2928
use LaravelJsonApi\Core\Query\FilterParameters;
3029
use LaravelJsonApi\Core\Query\IncludePaths;
3130
use LaravelJsonApi\Core\Query\SortFields;
31+
use LaravelJsonApi\Laravel\Exceptions\HttpNotAcceptableException;
3232
use LogicException;
33-
use Symfony\Component\HttpKernel\Exception\HttpException;
3433
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
3534
use function array_key_exists;
3635

@@ -336,10 +335,6 @@ protected function mediaTypes(): array
336335
*/
337336
protected function notAcceptable(): HttpExceptionInterface
338337
{
339-
return new HttpException(
340-
Response::HTTP_NOT_ACCEPTABLE,
341-
__("The requested resource is capable of generating only content not acceptable "
342-
. "according to the Accept headers sent in the request.")
343-
);
338+
return new HttpNotAcceptableException();
344339
}
345340
}

src/Http/Requests/ResourceRequest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
2323
use Illuminate\Contracts\Validation\Validator;
2424
use Illuminate\Database\Eloquent\Model;
25-
use Illuminate\Http\Response;
2625
use Illuminate\Support\Collection;
2726
use LaravelJsonApi\Contracts\Auth\Authorizer;
2827
use LaravelJsonApi\Contracts\Schema\Relation;
@@ -31,10 +30,10 @@
3130
use LaravelJsonApi\Core\Query\IncludePaths;
3231
use LaravelJsonApi\Core\Store\LazyRelation;
3332
use LaravelJsonApi\Core\Support\Str;
33+
use LaravelJsonApi\Laravel\Exceptions\HttpUnsupportedMediaTypeException;
3434
use LaravelJsonApi\Spec\RelationBuilder;
3535
use LaravelJsonApi\Spec\ResourceBuilder;
3636
use LogicException;
37-
use Symfony\Component\HttpKernel\Exception\HttpException;
3837
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
3938
use function array_key_exists;
4039

@@ -349,10 +348,7 @@ protected function isSupportedMediaType(): bool
349348
*/
350349
protected function unsupportedMediaType(): HttpExceptionInterface
351350
{
352-
return new HttpException(
353-
Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
354-
__('The request entity has a media type which the server or resource does not support.')
355-
);
351+
return new HttpUnsupportedMediaTypeException();
356352
}
357353

358354
/**

0 commit comments

Comments
 (0)
0