Skip to content

Commit 6bfb198

Browse files
author
Samuel Martins
committed
Fixed validation if clientException is not generated by guzzle client
1 parent 411e4e8 commit 6bfb198

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/ClientHandler.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,45 @@
77

88
trait ClientHandler
99
{
10-
public function clientException(ClientException $exception)
10+
public function clientException($exception)
1111
{
1212
$statusCode = 500;
1313
$title = 'client_exception';
14-
$code = config($this->configFile.'codes.client.default');
15-
$detail = __('exception::exceptions.client.unavailable');
14+
$detail = $exception->getMessage();
15+
$code = $this->getCode();
1616

17-
$requestHost = $exception->getRequest()->getUri()->getHost();
18-
$clientCausers = $this->clientExceptionCausers();
17+
if ($exception instanceof ClientException) {
18+
$requestHost = $exception->getRequest()->getUri()->getHost();
19+
$clientCausers = $this->clientExceptionCausers();
1920

20-
if ($clientCausers->isPagarme($requestHost)) {
21-
$code = config('json-exception-handler.codes.client.pagarme') ?? 'pagarme';
22-
} elseif ($clientCausers->isMailgun($requestHost)) {
23-
$code = config('json-exception-handler.codes.client.mailgun') ?? 'mailgun';
24-
}
25-
26-
if (App::environment('production')) {
27-
$detail = $detail.' #'.$code;
28-
} else {
2921
$response = $exception->getResponse();
30-
$detail = json_decode($response->getBody())->message;
31-
$statusCode = $response->getStatusCode();
22+
23+
if ($clientCausers->isPagarme($requestHost)) {
24+
$code = config('json-exception-handler.codes.client.pagarme') ?? 'pagarme';
25+
$errors = json_decode($response->getBody())->errors;
26+
27+
$firstErrorMessage = '';
28+
foreach ($errors as $error) {
29+
$firstErrorMessage = $error->message;
30+
break;
31+
}
32+
33+
$detailedError = $firstErrorMessage.' #'.$code;
34+
} elseif ($clientCausers->isMailgun($requestHost)) {
35+
$code = config('json-exception-handler.codes.client.mailgun') ?? 'mailgun';
36+
$detailedError = json_decode($response->getBody())->message.' #'.$code;
37+
} else {
38+
// Unknown error
39+
$code = config('json-exception-handler.codes.client.default');
40+
}
41+
42+
if (App::environment('production')) {
43+
$detail = __('exception::exceptions.client.unavailable').' #'.$code;
44+
} else {
45+
// Return more details about error
46+
$detail = $detailedError ?? $detail;
47+
$statusCode = $response->getStatusCode();
48+
}
3249
}
3350

3451
$error = [[

0 commit comments

Comments
 (0)