Skip to content

Commit fc83105

Browse files
committed
fixed exception in getStatusCode() after throw TransportExceptionInterface exception
1 parent e4362dc commit fc83105

File tree

6 files changed

+13
-1
lines changed

6 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- fixed exception in `getStatusCode()` after throw `TransportExceptionInterface` exception
6+
37
## 2.2.0 - 2025-05-27
48

59
- added `toArray()` method to convert JSON request/response data into an array for use in the logger's `request`, `response` contexts and `request_body` in info

src/Context/ResponseContext.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public function __construct(LoggableResponse $response)
1919

2020
public function getStatusCode(): int
2121
{
22-
return $this->response->getStatusCode();
22+
try {
23+
return $this->response->getStatusCode();
24+
} catch (\Throwable) {
25+
return 0;
26+
}
2327
}
2428

2529
public function getResponseTime(): ?\DateTimeInterface

tests/AmpHttpClientTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ public function testTimeout(): void
638638
'message' => 'Response content: "0 http://127.0.0.1:8057/timeout-long"',
639639
'request-content' => 'abc=def',
640640
'response-content' => null,
641+
'response-status-code' => 0,
641642
'info-error' => 'Idle timeout reached for "http://127.0.0.1:8057/timeout-long".',
642643
],
643644
];

tests/CurlHttpClientTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ public function testTimeout(): void
644644
'message' => 'Response content: "0 http://127.0.0.1:8057/timeout-long"',
645645
'request-content' => 'abc=def',
646646
'response-content' => null,
647+
'response-status-code' => 0,
647648
'info-error' => 'Idle timeout reached for "http://127.0.0.1:8057/timeout-long".',
648649
],
649650
];

tests/NativeHttpClientTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ public function testTimeout(): void
649649
'message' => 'Response content: "0 http://127.0.0.1:8057/timeout-long"',
650650
'request-content' => 'abc=def',
651651
'response-content' => null,
652+
'response-status-code' => 0,
652653
'info-error' => 'fopen(http://127.0.0.1:8057/timeout-long): Failed to open stream: HTTP request failed!',
653654
],
654655
];

tests/TestLogger.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function ($key) {
7575
$log['response-headers-content-type'] = $context['response']->getHeaders()['content-type'] ?? null;
7676
$log['response-time'] = $context['response']->getResponseTime()?->format(DATE_RFC3339_EXTENDED);
7777
$log['response-time-datetime'] = $context['response']->getResponseTime();
78+
$log['response-status-code'] = $context['response']->getStatusCode();
7879
}
7980

8081
if (isset($context['info']) && ($context['info'] instanceof InfoContextInterface)) {

0 commit comments

Comments
 (0)