sfelix-martins /
json-exception-handler
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SMartins\JsonHandler; |
||
| 4 | |||
| 5 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
| 6 | |||
| 7 | trait NotFoundHttpHandler |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Set response parameters to NotFoundHttpException. |
||
| 11 | * |
||
| 12 | * @param NotFoundHttpException $exception |
||
| 13 | */ |
||
| 14 | public function notFoundHttpException(NotFoundHttpException $exception) |
||
| 15 | { |
||
| 16 | $statuCode = $exception->getStatusCode(); |
||
| 17 | $error = [[ |
||
| 18 | 'status' => $statuCode, |
||
| 19 | 'code' => $this->getCode('not_found_http'), |
||
| 20 | 'source' => ['pointer' => $exception->getFile().':'.$exception->getLine()], |
||
| 21 | 'title' => $this->getDescription($exception), |
||
| 22 | 'detail' => $this->getNotFoundMessage($exception), |
||
| 23 | ]]; |
||
| 24 | |||
| 25 | $this->jsonApiResponse->setStatus($statuCode); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 26 | $this->jsonApiResponse->setErrors($error); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get message based on file. If file is RouteCollection return specific |
||
| 31 | * message. |
||
| 32 | * |
||
| 33 | * @param NotFoundHttpException $exception |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public function getNotFoundMessage(NotFoundHttpException $exception) |
||
| 37 | { |
||
| 38 | $message = ! empty($exception->getMessage()) ? $exception->getMessage() : class_basename($exception); |
||
| 39 | if (basename($exception->getFile()) === 'RouteCollection.php') { |
||
| 40 | $message = __('exception::exceptions.not_found_http.message'); |
||
| 41 | } |
||
| 42 | |||
| 43 | return $message; |
||
| 44 | } |
||
| 45 | } |
||
| 46 |