+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
+
}