|
6 | 6 |
|
7 | 7 | use ArrayObject; |
8 | 8 | use GuzzleHttp\Client; |
| 9 | +use GuzzleHttp\Exception\ConnectException; |
9 | 10 | use GuzzleHttp\Handler\MockHandler; |
10 | 11 | use GuzzleHttp\HandlerStack; |
11 | 12 | use GuzzleHttp\Promise\PromiseInterface; |
@@ -165,4 +166,47 @@ public function test_headers_propagation(): void |
165 | 166 | $this->client->get('/'); |
166 | 167 |
|
167 | 168 | } |
| 169 | + |
| 170 | + /** |
| 171 | + * @dataProvider exceptionProvider |
| 172 | + */ |
| 173 | + public function test_exceptions_enabled_sets_response_attributes($response, ?int $expected = null): void |
| 174 | + { |
| 175 | + $client = new Client([ |
| 176 | + 'handler' => $this->handlerStack, |
| 177 | + 'base_uri' => 'https://example.com/', |
| 178 | + 'http_errors' => true, |
| 179 | + 'exceptions' => true, |
| 180 | + ]); |
| 181 | + $this->mock->append($response); |
| 182 | + $this->assertCount(0, $this->storage); |
| 183 | + |
| 184 | + try { |
| 185 | + $client->send(new Request('GET', 'https://example.com/error')); |
| 186 | + } catch (\Exception $e) { |
| 187 | + // Expected exception |
| 188 | + } |
| 189 | + $this->assertCount(1, $this->storage); |
| 190 | + $span = $this->storage->offsetGet(0); |
| 191 | + $attributes = $span->getAttributes()->toArray(); |
| 192 | + if ($expected) { |
| 193 | + $this->assertSame($expected, $attributes[TraceAttributes::HTTP_RESPONSE_STATUS_CODE]); |
| 194 | + $this->assertGreaterThan(0, $attributes[TraceAttributes::HTTP_RESPONSE_BODY_SIZE]); |
| 195 | + $this->assertArrayHasKey(TraceAttributes::NETWORK_PROTOCOL_VERSION, $attributes); |
| 196 | + } else { |
| 197 | + $this->assertArrayNotHasKey(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $attributes); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + public static function exceptionProvider(): array |
| 202 | + { |
| 203 | + return [ |
| 204 | + '400 Bad Request' => [new Response(400, [], 'Bad Request'), 400], |
| 205 | + '404 Not Found' => [new Response(404, [], 'Not Found'), 404], |
| 206 | + '500 Internal Server Error' => [new Response(500, [], 'Internal Server Error'), 500], |
| 207 | + '503 Service Unavailable' => [new Response(503, [], 'Service Unavailable'), 503], |
| 208 | + 'network connection error' => [new ConnectException('network error', new Request('GET', 'https://example.com/error'))], |
| 209 | + 'runtime exception' => [new \RuntimeException('runtime error')], |
| 210 | + ]; |
| 211 | + } |
168 | 212 | } |
0 commit comments