Skip to content

Commit 8f355d9

Browse files
authored
Use throwable instead of exception (#31)
* Use throwable instead of exception * Use correct namespace * Remove superfloous typehinting * Use FQCN for Throwable * Added changelog
1 parent 64a67a6 commit 8f355d9

9 files changed

+47
-56
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
All Notable changes to `gmponos/guzzle_logger` will be documented in this file
44

5+
## 2.0.0 - 2020-07-05
6+
7+
### Changed
8+
- [BC] Changed the signature of `HandlerInterface::log` to allow Throwables. Now the signature is
9+
```php
10+
HandlerInterface::log(
11+
LoggerInterface $logger,
12+
RequestInterface $request,
13+
?ResponseInterface $response = null,
14+
?Throwable $exception = null,
15+
?TransferStats $stats = null,
16+
array $options = []
17+
)
18+
```
19+
520
## 1.1.0 - 2019-09-03
621

722
### Added

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ final class SimpleHandler implements HandlerInterface
8080
public function log(
8181
LoggerInterface $logger,
8282
RequestInterface $request,
83-
?ResponseInterface $response,
84-
?\Exception $exception,
85-
?TransferStats $stats,
86-
array $options
83+
?ResponseInterface $response = null,
84+
?\Throwable $exception = null,
85+
?TransferStats $stats = null,
86+
array $options = []
8787
): void {
8888
$logger->debug('Guzzle HTTP request: ' . \GuzzleHttp\Psr7\str($request));
8989
return;

src/Handler/HandlerInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace GuzzleLogMiddleware\Handler;
66

7-
use Exception;
87
use GuzzleHttp\TransferStats;
98
use Psr\Http\Message\RequestInterface;
109
use Psr\Http\Message\ResponseInterface;
1110
use Psr\Log\LoggerInterface;
11+
use Throwable;
1212

1313
/**
1414
* Classes that will implement this interface are responsible
15-
* to log the MessageInterface|\Exception|TransferStats that are
15+
* to log the MessageInterface|\Throwable|TransferStats that are
1616
* passed as values.
1717
*
1818
* @author George Mponos <gmponos@gmail.com>
@@ -22,9 +22,9 @@ interface HandlerInterface
2222
public function log(
2323
LoggerInterface $logger,
2424
RequestInterface $request,
25-
?ResponseInterface $response,
26-
?Exception $exception,
27-
?TransferStats $stats,
28-
array $options
25+
?ResponseInterface $response = null,
26+
?Throwable $exception = null,
27+
?TransferStats $stats = null,
28+
array $options = []
2929
): void;
3030
}

src/Handler/LogLevelStrategy/FixedStrategy.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ public function __construct(
5656
/**
5757
* Returns the log level.
5858
*
59-
* @param MessageInterface|\Exception|TransferStats $value
60-
* @param array $options
59+
* @param MessageInterface|\Throwable|TransferStats $value
6160
*/
6261
public function getLevel($value, array $options): string
6362
{
6463
if ($value instanceof RequestException) {
6564
return $this->defaultLevel;
6665
}
6766

68-
if ($value instanceof \Exception) {
67+
if ($value instanceof \Throwable) {
6968
return $this->exceptionLevel;
7069
}
7170

src/Handler/LogLevelStrategy/LogLevelStrategyInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ interface LogLevelStrategyInterface
3030
/**
3131
* Returns the log level.
3232
*
33-
* @param MessageInterface|\Exception|TransferStats $value
34-
* @param array $options
35-
* @return string The LogLevel
33+
* @param MessageInterface|\Throwable|TransferStats $value
3634
*/
3735
public function getLevel($value, array $options): string;
3836
}

src/Handler/LogLevelStrategy/StatusCodeStrategy.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace GuzzleLogMiddleware\Handler\LogLevelStrategy;
66

7-
use GuzzleHttp\TransferStats;
8-
use Psr\Http\Message\RequestInterface;
97
use Psr\Http\Message\ResponseInterface;
108
use Psr\Log\LogLevel;
119

@@ -53,7 +51,7 @@ public function setLevel(int $statusCode, string $level): void
5351
public function getLevel($value, array $options): string
5452
{
5553
$this->setOptions($options);
56-
if ($value instanceof \Exception) {
54+
if ($value instanceof \Throwable) {
5755
return $this->exceptionLevel;
5856
}
5957

src/Handler/LogLevelStrategy/ThresholdStrategy.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ final class ThresholdStrategy implements LogLevelStrategyInterface
5757
*
5858
* @param array $thresholds An array of thresholds.
5959
* @param string $defaultLevel The that will be used for the requests and as a default one.
60-
* @param string $exceptionLevel
6160
*/
6261
public function __construct(
6362
array $thresholds = [],
@@ -71,7 +70,7 @@ public function __construct(
7170

7271
public function getLevel($value, array $options): string
7372
{
74-
if ($value instanceof \Exception) {
73+
if ($value instanceof \Throwable) {
7574
return $this->exceptionLevel;
7675
}
7776

src/Handler/MultiRecordArrayHandler.php

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace GuzzleLogMiddleware\Handler;
66

7-
use Exception;
87
use GuzzleHttp\TransferStats;
98
use GuzzleLogMiddleware\Handler\LogLevelStrategy\LogLevelStrategyInterface;
109
use Psr\Http\Message\MessageInterface;
1110
use Psr\Http\Message\RequestInterface;
1211
use Psr\Http\Message\ResponseInterface;
1312
use Psr\Log\LoggerInterface;
13+
use Throwable;
1414

1515
/**
1616
* @author George Mponos <gmponos@gmail.com>
@@ -28,12 +28,14 @@ final class MultiRecordArrayHandler extends AbstractHandler
2828
private $summarySize;
2929

3030
/**
31-
* @param LogLevelStrategyInterface|null $logLevelStrategy
3231
* @param int $truncateSize If the body of the request/response is greater than the size of this integer the body will be truncated
3332
* @param int $summarySize The size to use for the summary of a truncated body
3433
*/
35-
public function __construct(LogLevelStrategyInterface $logLevelStrategy = null, int $truncateSize = 3500, int $summarySize = 200)
36-
{
34+
public function __construct(
35+
LogLevelStrategyInterface $logLevelStrategy = null,
36+
int $truncateSize = 3500,
37+
int $summarySize = 200
38+
) {
3739
$this->logLevelStrategy = $logLevelStrategy === null ? $this->getDefaultStrategy() : $logLevelStrategy;
3840
$this->truncateSize = $truncateSize;
3941
$this->summarySize = $summarySize;
@@ -42,10 +44,10 @@ public function __construct(LogLevelStrategyInterface $logLevelStrategy = null,
4244
public function log(
4345
LoggerInterface $logger,
4446
RequestInterface $request,
45-
?ResponseInterface $response,
46-
?Exception $exception,
47-
?TransferStats $stats,
48-
array $options
47+
?ResponseInterface $response = null,
48+
?Throwable $exception = null,
49+
?TransferStats $stats = null,
50+
array $options = []
4951
): void {
5052
$this->logRequest($logger, $request, $options);
5153

@@ -75,12 +77,6 @@ private function logRequest(LoggerInterface $logger, RequestInterface $request,
7577
$logger->log($level, 'Guzzle HTTP request', $context);
7678
}
7779

78-
/**
79-
* @param LoggerInterface $logger
80-
* @param ResponseInterface $response
81-
* @param array $options
82-
* @return void
83-
*/
8480
private function logResponse(LoggerInterface $logger, ResponseInterface $response, array $options): void
8581
{
8682
$context['response']['headers'] = $response->getHeaders();
@@ -96,13 +92,7 @@ private function logResponse(LoggerInterface $logger, ResponseInterface $respons
9692
$logger->log($level, 'Guzzle HTTP response', $context);
9793
}
9894

99-
/**
100-
* @param LoggerInterface $logger
101-
* @param Exception|null $exception
102-
* @param array $options
103-
* @return void
104-
*/
105-
private function logReason(LoggerInterface $logger, ?Exception $exception, array $options): void
95+
private function logReason(LoggerInterface $logger, ?Throwable $exception, array $options): void
10696
{
10797
if ($exception === null) {
10898
return;
@@ -117,12 +107,6 @@ private function logReason(LoggerInterface $logger, ?Exception $exception, array
117107
$logger->log($level, 'Guzzle HTTP exception', $context);
118108
}
119109

120-
/**
121-
* @param LoggerInterface $logger
122-
* @param TransferStats $stats
123-
* @param array $options
124-
* @return void
125-
*/
126110
private function logStats(LoggerInterface $logger, TransferStats $stats, array $options): void
127111
{
128112
$this->logLevelStrategy->getLevel($stats, $options);
@@ -133,8 +117,6 @@ private function logStats(LoggerInterface $logger, TransferStats $stats, array $
133117
}
134118

135119
/**
136-
* @param MessageInterface $message
137-
* @param array $options
138120
* @return string|array
139121
*/
140122
private function formatBody(MessageInterface $message, array $options)

src/Handler/StringHandler.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace GuzzleLogMiddleware\Handler;
66

7-
use Exception;
87
use GuzzleHttp\TransferStats;
98
use GuzzleLogMiddleware\Handler\LogLevelStrategy\LogLevelStrategyInterface;
109
use Psr\Http\Message\RequestInterface;
1110
use Psr\Http\Message\ResponseInterface;
1211
use Psr\Log\LoggerInterface;
12+
use Throwable;
1313

1414
/**
1515
* @author George Mponos <gmponos@gmail.com>
@@ -24,10 +24,10 @@ public function __construct(LogLevelStrategyInterface $logLevelStrategy = null)
2424
public function log(
2525
LoggerInterface $logger,
2626
RequestInterface $request,
27-
?ResponseInterface $response,
28-
?Exception $exception,
29-
?TransferStats $stats,
30-
array $options
27+
?ResponseInterface $response = null,
28+
?Throwable $exception = null,
29+
?TransferStats $stats = null,
30+
array $options = []
3131
): void {
3232
$this->logRequest($logger, $request, $options);
3333

@@ -73,7 +73,7 @@ private function logResponse(LoggerInterface $logger, ResponseInterface $value,
7373
$logger->log($level, 'Guzzle HTTP response:' . "\n" . $str);
7474
}
7575

76-
private function logReason(LoggerInterface $logger, Exception $exception, array $options): void
76+
private function logReason(LoggerInterface $logger, Throwable $exception, array $options): void
7777
{
7878
$level = $this->logLevelStrategy->getLevel($exception, $options);
7979
$logger->log($level, sprintf('Guzzle HTTP exception: %s', $exception->getMessage()), [

0 commit comments

Comments
 (0)