Issues (62)

src/ClientHandler.php (1 issue)

1
<?php
2
3
namespace SMartins\JsonHandler;
4
5
use Illuminate\Support\Facades\App;
6
use GuzzleHttp\Exception\ClientException;
7
8
trait ClientHandler
9
{
10
    public function clientException($exception)
11
    {
12
        $statusCode = 500;
13
        $title = 'client_exception';
14
        $detail = $exception->getMessage();
15
        $code = $this->getCode();
16
17
        if ($exception instanceof ClientException) {
18
            $requestHost = $exception->getRequest()->getUri()->getHost();
19
            $clientCausers = $this->clientExceptionCausers();
20
21
            $response = $exception->getResponse();
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
            }
49
        }
50
51
        $error = [[
52
            'status'    => $statusCode,
53
            'code'      => $code,
54
            'source'    => ['pointer' => $exception->getFile().':'.$exception->getLine()],
55
            'title'     => $title,
56
            'detail'    => $detail,
57
        ]];
58
59
        $this->jsonApiResponse->setStatus($statusCode);
0 ignored issues
show
Bug Best Practice introduced by
The property jsonApiResponse does not exist on SMartins\JsonHandler\ClientHandler. Did you maybe forget to declare it?
Loading history...
60
        $this->jsonApiResponse->setErrors($error);
61
    }
62
63
    public function clientExceptionCausers()
64
    {
65
        return new class() {
66
            const PAGARME_HOST = 'api.pagar.me';
67
68
            const MAILGUN_HOST = 'api.mailgun.net';
69
70
            public function isPagarme($host)
71
            {
72
                return self::PAGARME_HOST == $host;
73
            }
74
75
            public function isMailgun($host)
76
            {
77
                return self::MAILGUN_HOST == $host;
78
            }
79
        };
80
    }
81
}
82