@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | { |
| 17 | 17 | public function __construct( |
| 18 | 18 | private readonly ResponseFactoryInterface $responseFactory |
| 19 | - ) { |
|
| 19 | + ){ |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function renderException(Request $request, int $code, \Throwable $exception): Response |
@@ -25,10 +25,10 @@ discard block |
||
| 25 | 25 | $acceptItems = AcceptHeader::fromString($request->getHeaderLine('Accept'))->getAll(); |
| 26 | 26 | |
| 27 | 27 | $response = $this->responseFactory->createResponse($code); |
| 28 | - if ($acceptItems && $acceptItems[0]->getValue() === 'application/json') { |
|
| 28 | + if ($acceptItems && $acceptItems[0]->getValue() === 'application/json'){ |
|
| 29 | 29 | $response = $response->withHeader('Content-Type', 'application/json; charset=UTF-8'); |
| 30 | 30 | $response->getBody()->write(\json_encode(['status' => $code])); |
| 31 | - } else { |
|
| 31 | + }else{ |
|
| 32 | 32 | $response->getBody()->write("Error code: {$code}"); |
| 33 | 33 | } |
| 34 | 34 | |
@@ -25,10 +25,13 @@ |
||
| 25 | 25 | $acceptItems = AcceptHeader::fromString($request->getHeaderLine('Accept'))->getAll(); |
| 26 | 26 | |
| 27 | 27 | $response = $this->responseFactory->createResponse($code); |
| 28 | - if ($acceptItems && $acceptItems[0]->getValue() === 'application/json') { |
|
| 28 | + if ($acceptItems && $acceptItems[0]->getValue() === 'application/json') |
|
| 29 | + { |
|
| 29 | 30 | $response = $response->withHeader('Content-Type', 'application/json; charset=UTF-8'); |
| 30 | 31 | $response->getBody()->write(\json_encode(['status' => $code])); |
| 31 | - } else { |
|
| 32 | + } |
|
| 33 | + else |
|
| 34 | + { |
|
| 32 | 35 | $response->getBody()->write("Error code: {$code}"); |
| 33 | 36 | } |
| 34 | 37 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | private readonly RendererInterface $renderer, |
| 31 | 31 | private readonly ResponseFactoryInterface $responseFactory, |
| 32 | 32 | private readonly ExceptionHandlerInterface $errorHandler, |
| 33 | - ) { |
|
| 33 | + ){ |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -39,17 +39,17 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function process(Request $request, Handler $handler): Response |
| 41 | 41 | { |
| 42 | - try { |
|
| 42 | + try{ |
|
| 43 | 43 | return $handler->handle($request); |
| 44 | - } catch (ClientException|RouterException $e) { |
|
| 44 | + }catch (ClientException | RouterException $e){ |
|
| 45 | 45 | $code = $e instanceof ClientException ? $e->getCode() : 404; |
| 46 | - } catch (\Throwable $e) { |
|
| 46 | + }catch (\Throwable $e){ |
|
| 47 | 47 | $code = 500; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | $this->errorHandler->report($e); |
| 51 | 51 | |
| 52 | - if (!$this->suppressErrors->suppressed()) { |
|
| 52 | + if (!$this->suppressErrors->suppressed()){ |
|
| 53 | 53 | return $this->renderError($request, $e, $code); |
| 54 | 54 | } |
| 55 | 55 | |
@@ -67,8 +67,8 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | [$format, $renderer] = $this->getRenderer($this->errorHandler, $request); |
| 69 | 69 | |
| 70 | - if ($format !== null) { |
|
| 71 | - $response = $response->withHeader('Content-Type', $format . '; charset=UTF-8'); |
|
| 70 | + if ($format !== null){ |
|
| 71 | + $response = $response->withHeader('Content-Type', $format.'; charset=UTF-8'); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | $response->getBody()->write( |
@@ -87,12 +87,12 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | private function getRenderer(ExceptionHandlerInterface $handler, Request $request): array |
| 89 | 89 | { |
| 90 | - if ($request->hasHeader('Accept')) { |
|
| 90 | + if ($request->hasHeader('Accept')){ |
|
| 91 | 91 | $acceptItems = AcceptHeader::fromString($request->getHeaderLine('Accept'))->getAll(); |
| 92 | - foreach ($acceptItems as $item) { |
|
| 92 | + foreach ($acceptItems as $item){ |
|
| 93 | 93 | $format = $item->getValue(); |
| 94 | 94 | $renderer = $handler->getRenderer($format); |
| 95 | - if ($renderer !== null) { |
|
| 95 | + if ($renderer !== null){ |
|
| 96 | 96 | return [$format, $renderer]; |
| 97 | 97 | } |
| 98 | 98 | } |
@@ -39,17 +39,23 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function process(Request $request, Handler $handler): Response |
| 41 | 41 | { |
| 42 | - try { |
|
| 42 | + try |
|
| 43 | + { |
|
| 43 | 44 | return $handler->handle($request); |
| 44 | - } catch (ClientException|RouterException $e) { |
|
| 45 | + } |
|
| 46 | + catch (ClientException|RouterException $e) |
|
| 47 | + { |
|
| 45 | 48 | $code = $e instanceof ClientException ? $e->getCode() : 404; |
| 46 | - } catch (\Throwable $e) { |
|
| 49 | + } |
|
| 50 | + catch (\Throwable $e) |
|
| 51 | + { |
|
| 47 | 52 | $code = 500; |
| 48 | 53 | } |
| 49 | 54 | |
| 50 | 55 | $this->errorHandler->report($e); |
| 51 | 56 | |
| 52 | - if (!$this->suppressErrors->suppressed()) { |
|
| 57 | + if (!$this->suppressErrors->suppressed()) |
|
| 58 | + { |
|
| 53 | 59 | return $this->renderError($request, $e, $code); |
| 54 | 60 | } |
| 55 | 61 | |
@@ -67,7 +73,8 @@ discard block |
||
| 67 | 73 | |
| 68 | 74 | [$format, $renderer] = $this->getRenderer($this->errorHandler, $request); |
| 69 | 75 | |
| 70 | - if ($format !== null) { |
|
| 76 | + if ($format !== null) |
|
| 77 | + { |
|
| 71 | 78 | $response = $response->withHeader('Content-Type', $format . '; charset=UTF-8'); |
| 72 | 79 | } |
| 73 | 80 | |
@@ -87,12 +94,15 @@ discard block |
||
| 87 | 94 | */ |
| 88 | 95 | private function getRenderer(ExceptionHandlerInterface $handler, Request $request): array |
| 89 | 96 | { |
| 90 | - if ($request->hasHeader('Accept')) { |
|
| 97 | + if ($request->hasHeader('Accept')) |
|
| 98 | + { |
|
| 91 | 99 | $acceptItems = AcceptHeader::fromString($request->getHeaderLine('Accept'))->getAll(); |
| 92 | - foreach ($acceptItems as $item) { |
|
| 100 | + foreach ($acceptItems as $item) |
|
| 101 | + { |
|
| 93 | 102 | $format = $item->getValue(); |
| 94 | 103 | $renderer = $handler->getRenderer($format); |
| 95 | - if ($renderer !== null) { |
|
| 104 | + if ($renderer !== null) |
|
| 105 | + { |
|
| 96 | 106 | return [$format, $renderer]; |
| 97 | 107 | } |
| 98 | 108 | } |