Skip to content

Commit 69e1cc8

Browse files
simPodclaude
andauthored
Add httpStatusCode and clickHouseExceptionName to ServerError (#322)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 38fa28e commit 69e1cc8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Exception/ServerError.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@
1111

1212
final class ServerError extends Exception
1313
{
14+
private function __construct(
15+
string $message,
16+
int $code,
17+
public readonly int $httpStatusCode,
18+
public readonly string|null $clickHouseExceptionName,
19+
) {
20+
parent::__construct($message, $code);
21+
}
22+
1423
public static function fromResponse(ResponseInterface $response): self
1524
{
1625
$bodyContent = $response->getBody()->__toString();
1726

27+
$errorCode = preg_match('~^Code: (\d+). DB::Exception:~', $bodyContent, $codeMatches) === 1
28+
? (int) $codeMatches[1]
29+
: 0;
30+
31+
$exceptionName = preg_match('~\(([A-Z][A-Z_\d]+)\)~', $bodyContent, $nameMatches) === 1
32+
? $nameMatches[1]
33+
: null;
34+
1835
return new self(
1936
$bodyContent,
20-
code: preg_match('~^Code: (\\d+). DB::Exception:~', $bodyContent, $matches) === 1 ? (int) $matches[1] : 0,
37+
$errorCode,
38+
$response->getStatusCode(),
39+
$exceptionName,
2140
);
2241
}
2342
}

tests/Exception/ServerErrorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,22 @@ public function testParseCode(): void
2626
$serverError = ServerError::fromResponse($response);
2727

2828
self::assertSame(48, $serverError->getCode());
29+
self::assertSame(501, $serverError->httpStatusCode);
30+
self::assertSame('NOT_IMPLEMENTED', $serverError->clickHouseExceptionName);
31+
}
32+
33+
public function testParseWithoutExceptionName(): void
34+
{
35+
$psr17Factory = new Psr17Factory();
36+
$response = $psr17Factory->createResponse(500)
37+
->withBody(
38+
$psr17Factory->createStream('Some unknown error'),
39+
);
40+
41+
$serverError = ServerError::fromResponse($response);
42+
43+
self::assertSame(0, $serverError->getCode());
44+
self::assertSame(500, $serverError->httpStatusCode);
45+
self::assertNull($serverError->clickHouseExceptionName);
2946
}
3047
}

0 commit comments

Comments
 (0)