Skip to content

Commit 37711a7

Browse files
authored
Added logging on HTTP exception (#507)
* Added logging on HTTP exception * Fixed tests * Test fix * Added name to please "composer normalize" * Dont log in constructor * Mniir * PHPStan fix
1 parent 39a70d8 commit 37711a7

File tree

7 files changed

+49
-18
lines changed

7 files changed

+49
-18
lines changed

src/AbstractApi.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ abstract class AbstractApi
4848
*/
4949
private $signers;
5050

51+
/**
52+
* @var LoggerInterface
53+
*/
54+
private $logger;
55+
5156
/**
5257
* @param Configuration|array $configuration
5358
*/
@@ -60,13 +65,13 @@ public function __construct($configuration = [], ?CredentialProvider $credential
6065
}
6166

6267
$this->httpClient = $httpClient ?? HttpClient::create();
63-
$logger = $logger ?? new NullLogger();
68+
$this->logger = $logger ?? new NullLogger();
6469
$this->configuration = $configuration;
6570
$this->credentialProvider = $credentialProvider ?? new CacheProvider(new ChainProvider([
6671
new ConfigurationProvider(),
67-
new WebIdentityProvider($logger),
68-
new IniFileProvider($logger),
69-
new InstanceProvider($this->httpClient, $logger),
72+
new WebIdentityProvider($this->logger),
73+
new IniFileProvider($this->logger),
74+
new InstanceProvider($this->httpClient, $this->logger),
7075
]));
7176
}
7277

@@ -121,7 +126,7 @@ final protected function getResponse(Request $request, ?RequestContext $context
121126
]
122127
);
123128

124-
return new Response($response, $this->httpClient);
129+
return new Response($response, $this->httpClient, $this->logger);
125130
}
126131

127132
/**

src/Response.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use AsyncAws\Core\Stream\ResponseBodyResourceStream;
1717
use AsyncAws\Core\Stream\ResponseBodyStream;
1818
use AsyncAws\Core\Stream\ResultStream;
19+
use Psr\Log\LoggerInterface;
1920
use Symfony\Component\HttpClient\Exception\TransportException;
2021
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -61,10 +62,16 @@ class Response
6162
*/
6263
private $didThrow = false;
6364

64-
public function __construct(ResponseInterface $response, HttpClientInterface $httpClient)
65+
/**
66+
* @var LoggerInterface
67+
*/
68+
private $logger;
69+
70+
public function __construct(ResponseInterface $response, HttpClientInterface $httpClient, LoggerInterface $logger)
6571
{
6672
$this->httpResponse = $response;
6773
$this->httpClient = $httpClient;
74+
$this->logger = $logger;
6875
}
6976

7077
public function __destruct()
@@ -349,6 +356,20 @@ private function getResolveStatus(): bool
349356
/** @psalm-suppress PropertyTypeCoercion */
350357
$this->resolveResult = new $class(...$args);
351358
}
359+
360+
if ($this->resolveResult instanceof HttpException) {
361+
/** @var int $code */
362+
$code = $this->httpResponse->getInfo('http_code');
363+
/** @var string $url */
364+
$url = $this->httpResponse->getInfo('url');
365+
$this->logger->error(sprintf('HTTP %d returned for "%s".', $code, $url), [
366+
'aws_code' => $this->resolveResult->getAwsCode(),
367+
'aws_message' => $this->resolveResult->getAwsMessage(),
368+
'aws_type' => $this->resolveResult->getAwsType(),
369+
'aws_detail' => $this->resolveResult->getAwsDetail(),
370+
]);
371+
}
372+
352373
if ($this->resolveResult instanceof Exception) {
353374
$this->didThrow = true;
354375

src/Test/ResultMockFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use AsyncAws\Core\Result;
99
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
1010
use AsyncAws\Core\Waiter;
11+
use Psr\Log\NullLogger;
1112
use Symfony\Component\HttpClient\MockHttpClient;
1213

1314
/**
@@ -40,7 +41,7 @@ public static function createFailing(string $class, int $code, ?string $message
4041

4142
$httpResponse = new SimpleMockedResponse(\json_encode(['message' => $message]), ['content-type' => 'application/json'], $code);
4243
$client = new MockHttpClient($httpResponse);
43-
$response = new Response($client->request('POST', 'http://localhost'), $client);
44+
$response = new Response($client->request('POST', 'http://localhost'), $client, new NullLogger());
4445

4546
$reflectionClass = new \ReflectionClass($class);
4647

tests/Unit/Result/AssumeRoleResponseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AsyncAws\Core\Sts\Result\AssumeRoleResponse;
77
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
88
use PHPUnit\Framework\TestCase;
9+
use Psr\Log\NullLogger;
910
use Symfony\Component\HttpClient\MockHttpClient;
1011

1112
class AssumeRoleResponseTest extends TestCase
@@ -40,7 +41,7 @@ public function testAssumeRoleResponse(): void
4041
');
4142

4243
$client = new MockHttpClient($response);
43-
$result = new AssumeRoleResponse(new Response($client->request('POST', 'http://localhost'), $client));
44+
$result = new AssumeRoleResponse(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
4445

4546
self::assertSame('arn:aws:sts::123456789012:assumed-role/demo/TestAR', $result->getAssumedRoleUser()->getArn());
4647
self::assertSame('ARO123EXAMPLE123:TestAR', $result->getAssumedRoleUser()->getAssumedRoleId());

tests/Unit/Result/AssumeRoleWithWebIdentityResponseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AsyncAws\Core\Sts\Result\AssumeRoleWithWebIdentityResponse;
77
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
88
use AsyncAws\Core\Test\TestCase;
9+
use Psr\Log\NullLogger;
910
use Symfony\Component\HttpClient\MockHttpClient;
1011

1112
class AssumeRoleWithWebIdentityResponseTest extends TestCase
@@ -35,7 +36,7 @@ public function testAssumeRoleWithWebIdentityResponse(): void
3536
</AssumeRoleWithWebIdentityResponse>');
3637

3738
$client = new MockHttpClient($response);
38-
$result = new AssumeRoleWithWebIdentityResponse(new Response($client->request('POST', 'http://localhost'), $client));
39+
$result = new AssumeRoleWithWebIdentityResponse(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
3940

4041
// self::assertTODO(expected, $result->getCredentials());
4142
self::assertSame('amzn1.account.AF6RHO7KZU5XRVQJGXK6HB56KR2A', $result->getSubjectFromWebIdentityToken());

tests/Unit/Result/GetCallerIdentityResponseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AsyncAws\Core\Sts\Result\GetCallerIdentityResponse;
77
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
88
use PHPUnit\Framework\TestCase;
9+
use Psr\Log\NullLogger;
910
use Symfony\Component\HttpClient\MockHttpClient;
1011

1112
class GetCallerIdentityResponseTest extends TestCase
@@ -27,7 +28,7 @@ public function testGetCallerIdentityResponse(): void
2728
');
2829

2930
$client = new MockHttpClient($response);
30-
$result = new GetCallerIdentityResponse(new Response($client->request('POST', 'http://localhost'), $client));
31+
$result = new GetCallerIdentityResponse(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
3132

3233
self::assertStringContainsString('ARO123EXAMPLE123:my-role-session-name', $result->getUserId());
3334
self::assertStringContainsString('123456789012', $result->getAccount());

tests/Unit/ResultTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use AsyncAws\Core\Result;
1111
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
1212
use PHPUnit\Framework\TestCase;
13+
use Psr\Log\NullLogger;
1314
use Symfony\Component\HttpClient\MockHttpClient;
1415

1516
class ResultTest extends TestCase
@@ -58,7 +59,7 @@ public function testThrowExceptionDestruct()
5859
{
5960
$response = new SimpleMockedResponse('Bad request', [], 400);
6061
$client = new MockHttpClient($response);
61-
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client));
62+
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
6263

6364
$this->expectException(ClientException::class);
6465
unset($result);
@@ -68,7 +69,7 @@ public function testThrowExceptionOnlyOnce()
6869
{
6970
$response = new SimpleMockedResponse('Bad request', [], 400);
7071
$client = new MockHttpClient($response);
71-
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client));
72+
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
7273

7374
try {
7475
$result->resolve();
@@ -90,7 +91,7 @@ public function testWait()
9091
$results = [];
9192
$client = new MockHttpClient((function () { while (true) { yield new SimpleMockedResponse('OK', [], 200); } })());
9293
for ($i = 0; $i < 10; ++$i) {
93-
$results[] = new Result(new Response($client->request('POST', 'http://localhost'), $client));
94+
$results[] = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
9495
}
9596

9697
$counter = 0;
@@ -106,8 +107,8 @@ public function testWait()
106107
public function testWaitWithMixException()
107108
{
108109
$client = new MockHttpClient([new SimpleMockedResponse('Bad request', [], 400), new SimpleMockedResponse('OK', [], 200)]);
109-
$result1 = new Result(new Response($client->request('POST', 'http://localhost'), $client));
110-
$result2 = new Result(new Response($client->request('POST', 'http://localhost'), $client));
110+
$result1 = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
111+
$result2 = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
111112

112113
$counter = 0;
113114
foreach (Result::wait([$result1, $result2]) as $result) {
@@ -123,7 +124,7 @@ public function testWaitWithMixException()
123124
public function testWaitWithExceptionOnDestruct()
124125
{
125126
$client = new MockHttpClient(new SimpleMockedResponse('Bad request', [], 400));
126-
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client));
127+
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
127128

128129
foreach (Result::wait([$result]) as $r) {
129130
self::assertTrue($r->info()['resolved']);
@@ -136,7 +137,7 @@ public function testWaitWithExceptionOnDestruct()
136137
public function testWaitWithBody()
137138
{
138139
$client = new MockHttpClient(new SimpleMockedResponse('OK', [], 200));
139-
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client));
140+
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
140141

141142
foreach (Result::wait([$result], null, true) as $result) {
142143
self::assertTrue($result->info()['resolved']);
@@ -147,7 +148,7 @@ public function testWaitWithBody()
147148
public function testWaitWithBodyAfterManuallyResolve()
148149
{
149150
$client = new MockHttpClient(new SimpleMockedResponse('OK', [], 200));
150-
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client));
151+
$result = new Result(new Response($client->request('POST', 'http://localhost'), $client, new NullLogger()));
151152

152153
$result->resolve();
153154
self::assertTrue($result->info()['resolved']);

0 commit comments

Comments
 (0)