Skip to content

Commit 95cdb32

Browse files
committed
add error code to ClientException
1 parent 34ebc5b commit 95cdb32

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/FreshMail/Client.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use FreshMail\ApiV2\Factory\MonologFactory;
77
use GuzzleHttp\RequestOptions;
88
use Psr\Log\LoggerInterface;
9+
use function is_array;
10+
use function json_decode;
911

1012
/**
1113
* Class to make proper request (with authorization) to FreshMail Rest API V2
@@ -74,10 +76,16 @@ public function doRequest(string $uri, array $params = [])
7476
throw new UnauthorizedException('Request unauthorized');
7577
}
7678

79+
$code = 0;
80+
$res = json_decode($exception->getResponse()->getBody()->getContents());
81+
if (NULL !== $res && is_array($res->errors)) {
82+
$code = array_pop($res->errors)->code ?? 0;
83+
}
84+
7785
throw new \FreshMail\ApiV2\ClientException(sprintf(
7886
'Connection error, error message: %s',
7987
$exception->getResponse()->getBody()->getContents()
80-
));
88+
), $code);
8189
} catch (\GuzzleHttp\Exception\ConnectException $exception) {
8290
throw new ConnectionException(sprintf('Connection error, error message: '.$exception->getMessage()));
8391
}

0 commit comments

Comments
 (0)