|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Exceptions; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Flugg\Responder\Exceptions\Http\ApiException; |
|
7
|
|
|
use Flugg\Responder\Exceptions\Http\PageNotFoundException; |
|
8
|
|
|
use Flugg\Responder\Exceptions\Http\RelationNotFoundException; |
|
9
|
|
|
use Flugg\Responder\Exceptions\Http\ResourceNotFoundException; |
|
10
|
|
|
use Flugg\Responder\Exceptions\Http\UnauthenticatedException; |
|
11
|
|
|
use Flugg\Responder\Exceptions\Http\UnauthorizedException; |
|
12
|
|
|
use Flugg\Responder\Exceptions\Http\ValidationFailedException; |
|
13
|
|
|
use Flugg\Responder\Http\Responses\ErrorResponseBuilder; |
|
14
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
15
|
|
|
use Illuminate\Auth\AuthenticationException; |
|
16
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
17
|
|
|
use Illuminate\Database\Eloquent\RelationNotFoundException as BaseRelationNotFoundException; |
|
18
|
|
|
use Illuminate\Http\JsonResponse; |
|
19
|
|
|
use Illuminate\Validation\ValidationException; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A trait used by exception handlers to handle automatic handling of error responses. |
|
24
|
|
|
* |
|
25
|
|
|
* @package flugger/laravel-responder |
|
26
|
|
|
* @author Alexander Tømmerås <[email protected]> |
|
27
|
|
|
* @license The MIT License |
|
28
|
|
|
*/ |
|
29
|
|
|
trait HandlesApiErrors |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Convert a Laravel exception to an API exception. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Exception $exception |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
7 |
|
protected function transformException(Exception $exception) |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->transformHttpException($exception); |
|
40
|
5 |
|
$this->transformEloquentException($exception); |
|
41
|
3 |
|
$this->transformValidationException($exception); |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Convert a Laravel HTTP exception to an API exception. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Exception $exception |
|
48
|
|
|
* @return void |
|
49
|
|
|
* @throws \Flugg\Responder\Exceptions\Http\PageNotFoundException |
|
50
|
|
|
*/ |
|
51
|
7 |
|
protected function transformHttpException(Exception $exception) |
|
52
|
|
|
{ |
|
53
|
7 |
|
if ($exception instanceof AuthenticationException) { |
|
54
|
1 |
|
throw new UnauthenticatedException; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
if ($exception instanceof AuthorizationException) { |
|
58
|
1 |
|
throw new UnauthorizedException; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
if ($exception instanceof NotFoundHttpException) { |
|
62
|
|
|
throw new PageNotFoundException; |
|
63
|
|
|
} |
|
64
|
5 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Convert an Eloquent exception to an API exception. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Exception $exception |
|
70
|
|
|
* @return void |
|
71
|
|
|
* @throws \Flugg\Responder\Exceptions\Http\ResourceNotFoundException |
|
72
|
|
|
* @throws \Flugg\Responder\Exceptions\Http\RelationNotFoundException |
|
73
|
|
|
*/ |
|
74
|
5 |
|
protected function transformEloquentException(Exception $exception) |
|
75
|
|
|
{ |
|
76
|
5 |
|
if ($exception instanceof ModelNotFoundException) { |
|
77
|
1 |
|
throw new ResourceNotFoundException; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
if ($exception instanceof BaseRelationNotFoundException) { |
|
81
|
1 |
|
throw new RelationNotFoundException; |
|
82
|
|
|
} |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Convert a Laravel validation exception to an API exception. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Exception $exception |
|
89
|
|
|
* @return void |
|
90
|
|
|
* @throws \Flugg\Responder\Exceptions\Http\ValidationFailedException |
|
91
|
|
|
*/ |
|
92
|
3 |
|
protected function transformValidationException(Exception $exception) |
|
93
|
|
|
{ |
|
94
|
3 |
|
if ($exception instanceof ValidationException) { |
|
95
|
1 |
|
throw new ValidationFailedException($exception->validator); |
|
96
|
|
|
} |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Convert API exceptions to error responses. |
|
101
|
|
|
* |
|
102
|
|
|
* @param \Flugg\Responder\Exceptions\Http\ApiException $exception |
|
103
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
104
|
|
|
*/ |
|
105
|
1 |
|
protected function renderApiError(ApiException $exception): JsonResponse |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->container->make(ErrorResponseBuilder::class) |
|
108
|
1 |
|
->error($exception->errorCode(), $exception->message()) |
|
109
|
1 |
|
->data($exception->data()) |
|
110
|
1 |
|
->respond($exception->statusCode()); |
|
111
|
|
|
} |
|
112
|
|
|
} |