Repository navigation
Expand file tree
/
Copy pathStreamApiException.php
More file actions
107 lines (96 loc) · 3.37 KB
/
Copy pathStreamApiException.php
File metadata and controls
107 lines (96 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace GetStream\Exceptions;
/**
* Thrown when the Stream API returns an HTTP 4xx or 5xx response carrying an
* `APIError` envelope, or when an HTTP response was received but the body
* could not be parsed as `APIError`.
*
* Inherited from `\Exception`:
* getMessage(): string — `APIError.message`
* getCode(): int — HTTP status code.
* For the canonical `APIError.code`, call
* `getApiErrorCode()`.
* getPrevious(): ?\Throwable — underlying cause (e.g. JSON parse error)
*/
class StreamApiException extends StreamException
{
private int $statusCode;
private int $apiErrorCode;
/** @var array<string, string> */
private array $exceptionFields;
private bool $unrecoverable;
private string $rawResponseBody;
private ?string $moreInfo;
private mixed $details;
/**
* @param string $message APIError.message
* @param int $statusCode HTTP status (e.g. 400, 404, 500). Also
* what `getCode()` returns.
* @param int $code APIError.code (read via `getApiErrorCode()`)
* @param array<string, string> $exceptionFields APIError.exception_fields (empty when not validation)
* @param bool $unrecoverable APIError.unrecoverable
* @param string $rawResponseBody Raw HTTP response body
* @param string|null $moreInfo APIError.more_info
* @param mixed $details APIError.details
* @param \Throwable|null $previous Cause-chain link (e.g. JSON parse error)
*/
public function __construct(
string $message,
int $statusCode,
int $code = 0,
array $exceptionFields = [],
bool $unrecoverable = false,
string $rawResponseBody = '',
?string $moreInfo = null,
mixed $details = null,
?\Throwable $previous = null,
) {
// Pass $statusCode (not $code) to the parent so `getCode()` returns
// the HTTP status. The canonical `APIError.code` is exposed via
// `getApiErrorCode()`.
parent::__construct($message, $statusCode, $previous);
$this->statusCode = $statusCode;
$this->apiErrorCode = $code;
$this->exceptionFields = $exceptionFields;
$this->unrecoverable = $unrecoverable;
$this->rawResponseBody = $rawResponseBody;
$this->moreInfo = $moreInfo;
$this->details = $details;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* The canonical `APIError.code` from the Stream error envelope. Distinct
* from `getCode()` (HTTP status) and `getStatusCode()` (its alias).
*/
public function getApiErrorCode(): int
{
return $this->apiErrorCode;
}
/**
* @return array<string, string>
*/
public function getExceptionFields(): array
{
return $this->exceptionFields;
}
public function isUnrecoverable(): bool
{
return $this->unrecoverable;
}
public function getRawResponseBody(): string
{
return $this->rawResponseBody;
}
public function getMoreInfo(): ?string
{
return $this->moreInfo;
}
public function getDetails(): mixed
{
return $this->details;
}
}