|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
8
|
|
|
use Illuminate\Http\JsonResponse; |
|
9
|
|
|
use Flugg\Responder\Exceptions\ApiException; |
|
10
|
|
|
use Flugg\Responder\Exceptions\ResourceNotFoundException; |
|
11
|
|
|
use Flugg\Responder\Exceptions\UnauthorizedException; |
|
12
|
|
|
use Flugg\Responder\Exceptions\ValidationFailedException; |
|
13
|
|
|
use Flugg\Responder\Responder; |
|
14
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* You may apply this trait to your exceptions handler to give you access to methods |
|
18
|
|
|
* you may use to let the package catch and handle any API exceptions. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Laravel Responder |
|
21
|
|
|
* @author Alexander Tømmerås <[email protected]> |
|
22
|
|
|
* @license The MIT License |
|
23
|
|
|
*/ |
|
24
|
|
|
trait HandlesApiErrors |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Checks if the exception extends from the package API exception. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Exception $e |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function isApiError( Exception $e ):bool |
|
33
|
|
|
{ |
|
34
|
|
|
return $e instanceof ApiException; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Transforms and renders api responses. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Exception $e |
|
41
|
|
|
* @return JsonResponse |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function renderApiErrors( Exception $e ):JsonResponse |
|
44
|
|
|
{ |
|
45
|
|
|
$this->transformExceptions( $e ); |
|
46
|
|
|
|
|
47
|
|
|
if ( $e instanceof ApiException ) { |
|
48
|
|
|
return $this->renderApiResponse( $e ); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Checks if the application is currently in testing mode. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function isRunningTests():bool |
|
58
|
|
|
{ |
|
59
|
|
|
return app()->runningUnitTests(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Renders readable responses for console, useful for testing. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Exception $e |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function renderTestErrors( Exception $e ) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->renderConsoleResponse( $e ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Transform Laravel exceptions into API exceptions. |
|
75
|
|
|
* |
|
76
|
|
|
* @param Exception $e |
|
77
|
|
|
* @return void |
|
78
|
|
|
* @throws ResourceNotFoundException |
|
79
|
|
|
* @throws UnauthorizedException |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function transformExceptions( Exception $e ) |
|
82
|
|
|
{ |
|
83
|
|
|
if ( $e instanceof AuthorizationException ) { |
|
84
|
|
|
throw new UnauthorizedException(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ( $e instanceof ModelNotFoundException ) { |
|
88
|
|
|
throw new ResourceNotFoundException(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Render an exception into an API response. |
|
94
|
|
|
* |
|
95
|
|
|
* @param ApiException $e |
|
96
|
|
|
* @return JsonResponse |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function renderApiResponse( ApiException $e ):JsonResponse |
|
99
|
|
|
{ |
|
100
|
|
|
$message = $e instanceof ValidationFailedException ? $e->getValidationMessages() : $e->getMessage(); |
|
101
|
|
|
|
|
102
|
|
|
return app( Responder::class )->error( $e->getErrorCode(), $e->getStatusCode(), $message ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Render an exception into an HTTP response for the console, used for debugging in test mode. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Exception $e |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function renderConsoleResponse( Exception $e ) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->renderForConsole( new ConsoleOutput( ConsoleOutput::VERBOSITY_VERBOSE ), $e ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Render an exception to the console. |
|
118
|
|
|
* |
|
119
|
|
|
* |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract public function renderForConsole( $output, Exception $e ); |
|
122
|
|
|
} |