Skip to content

Commit 2a20ddf

Browse files
CopilotGrotax
andcommitted
Add HTTP status detail to server errors
Co-authored-by: Grotax <5429298+Grotax@users.noreply.github.com>
1 parent 39d5fea commit 2a20ddf

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Changed
11-
- Add isPermaLink=false support to setPublicId() - (#103)
11+
- Add isPermaLink=false support to setPublicId() (#103)
12+
- Return better error messages when remote responds with error code (#146)
1213

1314
### Fixed
1415
- fix: rewrite relative links in `content:encoded` and other optional elements, not only in `content`/`description` properties

‎src/FeedIo/Adapter/ServerErrorException.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ public function __construct(
1212
protected ResponseInterface $response,
1313
float $duration = 0
1414
) {
15-
parent::__construct(
16-
'internal server error',
17-
$duration
15+
$statusCode = $response->getStatusCode();
16+
$reasonPhrase = trim($response->getReasonPhrase());
17+
$message = sprintf(
18+
'Server responded with: %d%s',
19+
$statusCode,
20+
$reasonPhrase !== '' ? ' ' . $reasonPhrase : ''
1821
);
22+
23+
parent::__construct($message, $duration);
1924
}
2025

2126
public function getResponse(): ResponseInterface

‎tests/FeedIo/Adapter/Http/ClientTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ public function testGetResponseThrowsServerErrorOnServerError(): void
105105
->willReturn($psrResponse);
106106

107107
$this->expectException(ServerErrorException::class);
108+
$this->expectExceptionMessage('Server responded with: 500 Internal Server Error');
109+
110+
$this->client->getResponse('https://example.com/feed.xml');
111+
}
112+
113+
public function testGetResponseThrowsServerErrorOnForbidden(): void
114+
{
115+
$psrResponse = new PsrResponse(403, [], 'Forbidden');
116+
117+
$this->psrClient
118+
->expects($this->once())
119+
->method('sendRequest')
120+
->willReturn($psrResponse);
121+
122+
$this->expectException(ServerErrorException::class);
123+
$this->expectExceptionMessage('Server responded with: 403 Forbidden');
108124

109125
$this->client->getResponse('https://example.com/feed.xml');
110126
}

‎tests/FeedIo/ReaderTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ protected function getClientMock()
5252
protected function getFaultyClientMock()
5353
{
5454
$response = $this->createMock('Psr\Http\Message\ResponseInterface');
55+
$response->method('getStatusCode')->willReturn(403);
56+
$response->method('getReasonPhrase')->willReturn('Forbidden');
57+
5558
$client = $this->createMock('FeedIo\Adapter\ClientInterface');
5659
$client->expects($this->any())->method('getResponse')->willThrowException(
5760
new ServerErrorException($response, 0)

0 commit comments

Comments
 (0)