sfelix-martins /
json-exception-handler
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SMartins\JsonHandler; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use SMartins\JsonHandler\Responses\JsonApiResponse; |
||
| 7 | |||
| 8 | trait JsonHandler |
||
| 9 | { |
||
| 10 | use ValidationHandler, |
||
| 11 | ModelNotFoundHandler, |
||
| 12 | AuthorizationHandler, |
||
| 13 | NotFoundHttpHandler, |
||
| 14 | AuthenticationHandler, |
||
| 15 | OAuthServerHandler, |
||
| 16 | MissingScopeHandler, |
||
| 17 | BadRequestHttpHandler, |
||
| 18 | ClientHandler, |
||
| 19 | CieloRequestHandler; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Config file name. |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $configFile = 'json-exception-handler'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * JsonApiResponse instance used on another traits to set response. |
||
| 29 | * @var SMartins\JsonHandler\Responses\JsonApiResponse; |
||
| 30 | */ |
||
| 31 | public $jsonApiResponse; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Receive exception instance to be used on methods. |
||
| 35 | * @var Exception |
||
| 36 | */ |
||
| 37 | private $exception; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Set the default response on $response attribute. Get default value from |
||
| 41 | * methods. |
||
| 42 | */ |
||
| 43 | public function setDefaultResponse() |
||
| 44 | { |
||
| 45 | $error = [[ |
||
| 46 | 'status' => $this->getStatusCode(), |
||
| 47 | 'code' => $this->getCode(), |
||
| 48 | 'source' => ['pointer' => $this->getDescription()], |
||
| 49 | 'title' => strtolower(class_basename($this->exception)), |
||
| 50 | 'detail' => $this->getMessage(), |
||
| 51 | ]]; |
||
| 52 | |||
| 53 | $this->jsonApiResponse->setStatus($this->getStatusCode()); |
||
| 54 | $this->jsonApiResponse->setErrors($error); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get default message from exception. |
||
| 59 | * |
||
| 60 | * @return string Exception message |
||
| 61 | */ |
||
| 62 | public function getMessage() |
||
| 63 | { |
||
| 64 | return $this->exception->getMessage(); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Mount the description with exception class, line and file. |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public function getDescription() |
||
| 73 | { |
||
| 74 | return class_basename($this->exception). |
||
| 75 | ' line '.$this->exception->getLine(). |
||
| 76 | ' in '.$this->exception->getFile(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get default http code. Check if exception has getStatusCode() methods. |
||
| 81 | * If not get from config file. |
||
| 82 | * |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | public function getStatusCode() |
||
| 86 | { |
||
| 87 | if (method_exists($this->exception, 'getStatusCode')) { |
||
| 88 | $httpCode = $this->exception->getStatusCode(); |
||
| 89 | } else { |
||
| 90 | $httpCode = config($this->configFile.'.http_code'); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $httpCode; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get error code. If code is empty from config file based on type. |
||
| 98 | * |
||
| 99 | * @param string $type Code type from config file |
||
| 100 | * @return int |
||
| 101 | */ |
||
| 102 | public function getCode($type = 'default') |
||
| 103 | { |
||
| 104 | $code = $this->exception->getCode(); |
||
| 105 | if (empty($this->exception->getCode())) { |
||
| 106 | $code = config($this->configFile.'.codes.'.$type); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $code; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Handle the json response. Check if exception is treated. If true call |
||
| 114 | * the specific handler. If false set the default response to be returned. |
||
| 115 | * |
||
| 116 | * @param Exception $exception |
||
| 117 | * @return JsonResponse |
||
| 118 | */ |
||
| 119 | public function jsonResponse(Exception $exception) |
||
| 120 | { |
||
| 121 | $this->exception = $exception; |
||
| 122 | $this->jsonApiResponse = new JsonApiResponse; |
||
|
0 ignored issues
–
show
|
|||
| 123 | |||
| 124 | if ($this->exceptionIsTreated()) { |
||
| 125 | $this->callExceptionHandler(); |
||
| 126 | } else { |
||
| 127 | $this->setDefaultResponse(); |
||
| 128 | } |
||
| 129 | |||
| 130 | return response()->json( |
||
| 131 | $this->jsonApiResponse->toArray(), |
||
| 132 | $this->jsonApiResponse->getStatus() |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if method to treat exception exists. |
||
| 138 | * |
||
| 139 | * @param Exception $exception The exception to be checked |
||
| 140 | * @return bool If method is callable |
||
| 141 | */ |
||
| 142 | public function exceptionIsTreated() |
||
| 143 | { |
||
| 144 | return is_callable([$this, $this->methodName()]); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Call the exception handler after of to check if the method exists. |
||
| 149 | * |
||
| 150 | * @param Exception $exception |
||
| 151 | * @return void Call the method |
||
| 152 | */ |
||
| 153 | public function callExceptionHandler() |
||
| 154 | { |
||
| 155 | $this->{$this->methodName()}($this->exception); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The method name is the exception name with first letter in lower case. |
||
| 160 | * |
||
| 161 | * @param Exception $exception |
||
| 162 | * @return string The method name |
||
| 163 | */ |
||
| 164 | public function methodName() |
||
| 165 | { |
||
| 166 | return lcfirst(class_basename($this->exception)); |
||
| 167 | } |
||
| 168 | } |
||
| 169 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..