Skip to content

Commit 395826c

Browse files
authored
fix: get status_code for http exceptions (#5)
1 parent ae8c2ac commit 395826c

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/Middleware/MetricMiddleware.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Psr\Http\Message\ResponseInterface;
1212
use Psr\Http\Message\ServerRequestInterface;
1313
use Psr\Http\Server\RequestHandlerInterface;
14+
use Swoole\Http\Status;
1415
use Throwable;
1516

1617
class MetricMiddleware extends AbstractMiddleware
@@ -41,7 +42,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4142
return $response;
4243
} catch (Throwable $exception) {
4344
$attributes[ErrorAttributes::ERROR_TYPE] = get_class($exception);
44-
$attributes[HttpAttributes::HTTP_RESPONSE_STATUS_CODE] = 500;
45+
$attributes[HttpAttributes::HTTP_RESPONSE_STATUS_CODE] = $this->getHttpStatusCodeForException($exception);
4546

4647
throw $exception;
4748
} finally {
@@ -57,4 +58,12 @@ protected function featureName(): string
5758
{
5859
return 'client_request';
5960
}
61+
62+
protected function getHttpStatusCodeForException(Throwable $exception): int
63+
{
64+
$exceptionCode = $exception->getCode();
65+
$isHttpExceptionCode = is_int($exceptionCode) && Status::getReasonPhrase($exceptionCode) !== 'Unknown';
66+
67+
return $isHttpExceptionCode ? $exceptionCode : Status::INTERNAL_SERVER_ERROR;
68+
}
6069
}

tests/Unit/Middleware/MetricMiddlewareTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ public function testProcessWithMetricsDisabled(): void
143143
$this->assertSame($this->response, $result);
144144
}
145145

146-
public function testProcessWithException(): void
146+
/**
147+
* @dataProvider exceptionCodeProvider
148+
*/
149+
public function testProcessWithException(int $exceptionCode, int $expectedStatusCode): void
147150
{
148151
$path = '/api/error';
149152
$method = 'POST';
150-
$exception = new RuntimeException('Test exception');
153+
$exception = new RuntimeException('Test exception', $exceptionCode);
151154

152155
$this->uri->method('getPath')->willReturn($path);
153156
$this->request->method('getMethod')->willReturn($method);
@@ -169,7 +172,7 @@ public function testProcessWithException(): void
169172
HttpAttributes::HTTP_ROUTE => $path,
170173
HttpAttributes::HTTP_REQUEST_METHOD => $method,
171174
ErrorAttributes::ERROR_TYPE => RuntimeException::class,
172-
HttpAttributes::HTTP_RESPONSE_STATUS_CODE => 500,
175+
HttpAttributes::HTTP_RESPONSE_STATUS_CODE => $expectedStatusCode,
173176
]
174177
);
175178

@@ -185,6 +188,24 @@ public function testProcessWithException(): void
185188
$middleware->process($this->request, $handler);
186189
}
187190

191+
public static function exceptionCodeProvider(): array
192+
{
193+
return [
194+
'Default exception code' => [
195+
'exceptionCode' => 0,
196+
'expectedStatusCode' => 500,
197+
],
198+
'Http exception code' => [
199+
'exceptionCode' => 422,
200+
'expectedStatusCode' => 422,
201+
],
202+
'Custom exception code' => [
203+
'exceptionCode' => 1000,
204+
'expectedStatusCode' => 500,
205+
],
206+
];
207+
}
208+
188209
private function configureRequestMock(string $method, string $path): void
189210
{
190211
$this->uri->method('getPath')->willReturn($path);

0 commit comments

Comments
 (0)