1 | <?php |
||
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) |
|
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 |
|
90 | } |
||
91 |