+5
src/Client/Public/PublicClient.php
+5
src/Client/Public/PublicClient.php
···
3
3
namespace SocialDept\AtpClient\Client\Public;
4
4
5
5
use Illuminate\Support\Facades\Http;
6
+
use SocialDept\AtpClient\Exceptions\AtpResponseException;
6
7
use SocialDept\AtpClient\Http\Response;
7
8
8
9
class PublicClient
···
17
18
$params = array_filter($params, fn ($v) => !is_null($v));
18
19
19
20
$response = Http::get($url, $params);
21
+
22
+
if ($response->failed() || isset($response->json()['error'])) {
23
+
throw AtpResponseException::fromResponse($response, $endpoint);
24
+
}
20
25
21
26
return new Response($response);
22
27
}
+31
src/Exceptions/AtpResponseException.php
+31
src/Exceptions/AtpResponseException.php
···
1
+
<?php
2
+
3
+
namespace SocialDept\AtpClient\Exceptions;
4
+
5
+
use Illuminate\Http\Client\Response;
6
+
7
+
class AtpResponseException extends \Exception
8
+
{
9
+
public function __construct(
10
+
public readonly string $errorCode,
11
+
public readonly string $errorMessage,
12
+
public readonly int $httpStatus,
13
+
public readonly string $endpoint,
14
+
public readonly array $responseBody,
15
+
) {
16
+
parent::__construct("{$errorCode}: {$errorMessage}", $httpStatus);
17
+
}
18
+
19
+
public static function fromResponse(Response $response, string $endpoint): self
20
+
{
21
+
$body = $response->json() ?? [];
22
+
23
+
return new self(
24
+
errorCode: $body['error'] ?? 'UnknownError',
25
+
errorMessage: $body['message'] ?? $response->body(),
26
+
httpStatus: $response->status(),
27
+
endpoint: $endpoint,
28
+
responseBody: $body,
29
+
);
30
+
}
31
+
}
+9
src/Http/HasHttp.php
+9
src/Http/HasHttp.php
···
7
7
use InvalidArgumentException;
8
8
use SocialDept\AtpClient\Auth\ScopeChecker;
9
9
use SocialDept\AtpClient\Enums\Scope;
10
+
use SocialDept\AtpClient\Exceptions\AtpResponseException;
10
11
use SocialDept\AtpClient\Exceptions\ValidationException;
11
12
use SocialDept\AtpClient\Session\Session;
12
13
use SocialDept\AtpClient\Session\SessionManager;
···
44
45
'DELETE' => $request->delete($url, $params),
45
46
default => throw new InvalidArgumentException("Unsupported method: {$method}"),
46
47
};
48
+
49
+
if ($response->failed() || isset($response->json()['error'])) {
50
+
throw AtpResponseException::fromResponse($response, $endpoint);
51
+
}
47
52
48
53
if (config('atp-client.schema_validation') && Schema::exists($endpoint)) {
49
54
$this->validateResponse($endpoint, $response);
···
129
134
$response = $this->buildAuthenticatedRequest($session, $url, 'POST')
130
135
->withBody($data, $mimeType)
131
136
->post($url);
137
+
138
+
if ($response->failed() || isset($response->json()['error'])) {
139
+
throw AtpResponseException::fromResponse($response, $endpoint);
140
+
}
132
141
133
142
return new Response($response);
134
143
}