1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Flugg\Responder\Contracts\Responder; |
7
|
|
|
use Flugg\Responder\Exceptions\Http\HttpException; |
8
|
|
|
use Flugg\Responder\Exceptions\Http\PageNotFoundException; |
9
|
|
|
use Flugg\Responder\Exceptions\Http\RelationNotFoundException; |
10
|
|
|
use Flugg\Responder\Exceptions\Http\UnauthenticatedException; |
11
|
|
|
use Flugg\Responder\Exceptions\Http\UnauthorizedException; |
12
|
|
|
use Flugg\Responder\Exceptions\Http\ValidationFailedException; |
13
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
14
|
|
|
use Illuminate\Auth\AuthenticationException; |
15
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
16
|
|
|
use Illuminate\Database\Eloquent\RelationNotFoundException as BaseRelationNotFoundException; |
17
|
|
|
use Illuminate\Http\JsonResponse; |
18
|
|
|
use Illuminate\Validation\ValidationException; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A trait used by exception handlers to transform and render error responses. |
23
|
|
|
* |
24
|
|
|
* @package flugger/laravel-responder |
25
|
|
|
* @author Alexander Tømmerås <[email protected]> |
26
|
|
|
* @license The MIT License |
27
|
|
|
*/ |
28
|
|
|
trait ConvertsExceptions |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* A list of default exception types that should not be converted. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $dontConvert = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Convert an exception to another exception |
39
|
|
|
* |
40
|
|
|
* @param \Exception|\Throwable $exception |
41
|
|
|
* @param array $convert |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
7 |
|
protected function convert($exception, array $convert) |
45
|
|
|
{ |
46
|
7 |
|
foreach ($convert as $source => $target) { |
47
|
7 |
|
if ($exception instanceof $source) { |
48
|
5 |
|
if (is_callable($target)) { |
49
|
1 |
|
$target($exception); |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
throw new $target; |
53
|
|
|
} |
54
|
|
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convert a default exception to an API exception. |
59
|
|
|
* |
60
|
|
|
* @param \Exception|\Throwable $exception |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
7 |
|
protected function convertDefaultException($exception) |
64
|
|
|
{ |
65
|
7 |
|
$this->convert($exception, array_diff_key([ |
66
|
7 |
|
AuthenticationException::class => UnauthenticatedException::class, |
67
|
|
|
AuthorizationException::class => UnauthorizedException::class, |
68
|
|
|
NotFoundHttpException::class => PageNotFoundException::class, |
69
|
|
|
ModelNotFoundException::class => PageNotFoundException::class, |
70
|
|
|
BaseRelationNotFoundException::class => RelationNotFoundException::class, |
71
|
|
|
ValidationException::class => function ($exception) { |
72
|
1 |
|
throw new ValidationFailedException($exception->validator); |
73
|
7 |
|
}, |
74
|
7 |
|
], array_flip($this->dontConvert))); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Render an error response from an API exception. |
79
|
|
|
* |
80
|
|
|
* @param \Flugg\Responder\Exceptions\Http\HttpException $exception |
81
|
|
|
* @return \Illuminate\Http\JsonResponse |
82
|
|
|
*/ |
83
|
1 |
|
protected function renderResponse(HttpException $exception): JsonResponse |
84
|
|
|
{ |
85
|
1 |
|
return app(Responder::class) |
86
|
1 |
|
->error($exception->errorCode(), $exception->message()) |
87
|
1 |
|
->data($exception->data()) |
88
|
1 |
|
->respond($exception->statusCode(), $exception->getHeaders()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|