Skip to content

Commit

Permalink
Add error message to failed requests (#1722)
Browse files Browse the repository at this point in the history
Co-authored-by: sc <sc@local>
Co-authored-by: Alex Bouma <alex@bouma.me>
Co-authored-by: Michi Hoffmann <cleptric@users.noreply.github.com>
  • Loading branch information
4 people committed May 24, 2024
1 parent 2ceeca4 commit 7ed2844
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function sendRequest(Request $request, Options $options): Response
curl_setopt($curlHandle, \CURLOPT_PROXYUSERPWD, $httpProxyAuthentication);
}

/** @var string|false $body */
$body = curl_exec($curlHandle);

if ($body === false) {
Expand All @@ -105,6 +106,8 @@ public function sendRequest(Request $request, Options $options): Response

curl_close($curlHandle);

return new Response($statusCode, $responseHeaders, '');
$error = $statusCode >= 400 ? $body : '';

return new Response($statusCode, $responseHeaders, $error);
}
}
23 changes: 23 additions & 0 deletions tests/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,32 @@ public function testClientMakesUncompressedRequestWhenCompressionDisabled(): voi
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($response->getStatusCode(), $serverOutput['status']);
$this->assertEquals($request->getStringBody(), $serverOutput['body']);
$this->assertEquals($response->getError(), '');
$this->assertEquals(\strlen($request->getStringBody()), $serverOutput['headers']['Content-Length']);
}

public function testClientReturnsBodyAsErrorOnNonSuccessStatusCode(): void
{
$testServer = $this->startTestServer();

$options = new Options([
'dsn' => "http://publicKey@{$testServer}/400",
]);

$request = new Request();
$request->setStringBody('test');

$client = new HttpClient('sentry.php', 'testing');
$response = $client->sendRequest($request, $options);

$this->stopTestServer();

$this->assertFalse($response->isSuccess());
$this->assertEquals(400, $response->getStatusCode());

$this->assertEquals($request->getStringBody(), $response->getError());
}

public function testThrowsExceptionIfDsnOptionIsNotSet(): void
{
$this->expectException(\RuntimeException::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/testserver/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@

http_response_code($status);

return 'Processed.';
echo $body;

0 comments on commit 7ed2844

Please sign in to comment.