@@ -71,22 +71,32 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | public function process(Request $request, Handler $handler): Response |
| 73 | 73 | { |
| 74 | - try { |
|
| 74 | + try |
|
| 75 | + { |
|
| 75 | 76 | return $handler->handle($request); |
| 76 | - } catch (ClientException | RouterException $e) { |
|
| 77 | - if ($e instanceof ClientException) { |
|
| 77 | + } |
|
| 78 | + catch (ClientException | RouterException $e) |
|
| 79 | + { |
|
| 80 | + if ($e instanceof ClientException) |
|
| 81 | + { |
|
| 78 | 82 | $code = $e->getCode(); |
| 79 | - } else { |
|
| 83 | + } |
|
| 84 | + else |
|
| 85 | + { |
|
| 80 | 86 | $code = 404; |
| 81 | 87 | } |
| 82 | - } catch (\Throwable $e) { |
|
| 88 | + } |
|
| 89 | + catch (\Throwable $e) |
|
| 90 | + { |
|
| 83 | 91 | $snapshotter = $this->getOptional(SnapshotterInterface::class); |
| 84 | - if ($snapshotter !== null) { |
|
| 92 | + if ($snapshotter !== null) |
|
| 93 | + { |
|
| 85 | 94 | /** @var SnapshotterInterface $snapshotter */ |
| 86 | 95 | $snapshotter->register($e); |
| 87 | 96 | } |
| 88 | 97 | |
| 89 | - if (!$this->suppressErrors) { |
|
| 98 | + if (!$this->suppressErrors) |
|
| 99 | + { |
|
| 90 | 100 | return $this->renderError($request, $e); |
| 91 | 101 | } |
| 92 | 102 | |
@@ -109,7 +119,8 @@ discard block |
||
| 109 | 119 | { |
| 110 | 120 | $response = $this->responseFactory->createResponse(500); |
| 111 | 121 | |
| 112 | - if ($request->getHeaderLine('Accept') == 'application/json') { |
|
| 122 | + if ($request->getHeaderLine('Accept') == 'application/json') |
|
| 123 | + { |
|
| 113 | 124 | $response = $response->withHeader('Content-Type', 'application/json'); |
| 114 | 125 | $handler = new JsonHandler(); |
| 115 | 126 | $response->getBody()->write(json_encode( |
@@ -119,10 +130,13 @@ discard block |
||
| 119 | 130 | true |
| 120 | 131 | ) |
| 121 | 132 | )); |
| 122 | - } else { |
|
| 133 | + } |
|
| 134 | + else |
|
| 135 | + { |
|
| 123 | 136 | $handler = new HtmlHandler(); |
| 124 | 137 | $state = $this->getOptional(StateInterface::class); |
| 125 | - if ($state !== null) { |
|
| 138 | + if ($state !== null) |
|
| 139 | + { |
|
| 126 | 140 | $handler = $handler->withState($state); |
| 127 | 141 | } |
| 128 | 142 | |
@@ -156,9 +170,12 @@ discard block |
||
| 156 | 170 | */ |
| 157 | 171 | private function getOptional(string $class) |
| 158 | 172 | { |
| 159 | - try { |
|
| 173 | + try |
|
| 174 | + { |
|
| 160 | 175 | return $this->container->get($class); |
| 161 | - } catch (\Throwable | ContainerExceptionInterface $se) { |
|
| 176 | + } |
|
| 177 | + catch (\Throwable | ContainerExceptionInterface $se) |
|
| 178 | + { |
|
| 162 | 179 | return null; |
| 163 | 180 | } |
| 164 | 181 | } |
@@ -40,9 +40,12 @@ |
||
| 40 | 40 | public function renderException(Request $request, int $code, string $message): Response |
| 41 | 41 | { |
| 42 | 42 | $response = $this->responseFactory->createResponse($code); |
| 43 | - if ($request->getHeaderLine('Accept') == 'application/json') { |
|
| 43 | + if ($request->getHeaderLine('Accept') == 'application/json') |
|
| 44 | + { |
|
| 44 | 45 | $response->getBody()->write(json_encode(['status' => $code, 'error' => $message])); |
| 45 | - } else { |
|
| 46 | + } |
|
| 47 | + else |
|
| 48 | + { |
|
| 46 | 49 | $response->getBody()->write("Error code: {$code}"); |
| 47 | 50 | } |
| 48 | 51 | |