abterphp /
admin
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Admin\Http\Controllers; |
||
| 6 | |||
| 7 | use Opulence\Http\Responses\Response; |
||
| 8 | use Opulence\Http\Responses\ResponseHeaders; |
||
| 9 | |||
| 10 | trait ApiIssueTrait |
||
| 11 | { |
||
| 12 | protected string $problemBaseUrl; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param string $msg |
||
| 16 | * @param array $errors |
||
| 17 | * |
||
| 18 | * @return Response |
||
| 19 | */ |
||
| 20 | protected function handleErrors(string $msg, array $errors): Response |
||
| 21 | { |
||
| 22 | // @phan-suppress-next-line PhanUndeclaredProperty |
||
| 23 | $this->logger->debug($msg); |
||
| 24 | |||
| 25 | $detail = []; |
||
| 26 | foreach ($errors as $key => $keyErrors) { |
||
| 27 | foreach ($keyErrors as $keyError) { |
||
| 28 | $detail[] = sprintf('%s: %s', $key, $keyError); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | $status = ResponseHeaders::HTTP_BAD_REQUEST; |
||
| 33 | $content = [ |
||
| 34 | 'type' => sprintf('%sbad-request', $this->problemBaseUrl), |
||
| 35 | 'title' => 'Bad Request', |
||
| 36 | 'status' => $status, |
||
| 37 | 'detail' => implode("\n", $detail), |
||
| 38 | ]; |
||
| 39 | |||
| 40 | $response = new Response(); |
||
| 41 | $response->setStatusCode($status); |
||
| 42 | $response->setContent(json_encode($content)); |
||
| 43 | |||
| 44 | return $response; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $msg |
||
| 49 | * @param \Exception $exception |
||
| 50 | * |
||
| 51 | * @return Response |
||
| 52 | */ |
||
| 53 | protected function handleException(string $msg, \Exception $exception): Response |
||
| 54 | { |
||
| 55 | // @phan-suppress-next-line PhanUndeclaredProperty |
||
| 56 | $this->logger->error($msg, $this->getExceptionContext($exception)); |
||
| 57 | |||
| 58 | $status = ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR; |
||
| 59 | $content = [ |
||
| 60 | 'type' => sprintf('%sinternal-server-error', $this->problemBaseUrl), |
||
| 61 | 'title' => 'Internal Server Error', |
||
| 62 | 'status' => $status, |
||
| 63 | 'detail' => $exception->getMessage(), |
||
| 64 | ]; |
||
| 65 | |||
| 66 | $response = new Response(); |
||
| 67 | $response->setStatusCode($status); |
||
| 68 | $response->setContent(json_encode($content)); |
||
| 69 | |||
| 70 | return $response; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param \Exception $exception |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | protected function getExceptionContext(\Exception $exception): array |
||
| 79 | { |
||
| 80 | // @phan-suppress-next-line PhanUndeclaredConstantOfClass |
||
| 81 | $result = [static::LOG_CONTEXT_EXCEPTION => $exception->getMessage()]; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 82 | |||
| 83 | $i = 1; |
||
| 84 | while ($exception = $exception->getPrevious()) { |
||
| 85 | // @phan-suppress-next-line PhanUndeclaredConstantOfClass |
||
| 86 | $result[sprintf(static::LOG_PREVIOUS_EXCEPTION, $i++)] = $exception->getMessage(); |
||
|
0 ignored issues
–
show
|
|||
| 87 | } |
||
| 88 | |||
| 89 | return $result; |
||
| 90 | } |
||
| 91 | } |
||
| 92 |