ErrorException.php
1.43 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
<?php
declare(strict_types=1);
namespace OpenAI\Exceptions;
use Exception;
use Psr\Http\Message\ResponseInterface;
final class ErrorException extends Exception
{
private readonly int $statusCode;
/**
* Creates a new Exception instance.
*
* @param array{message: string|array<int, string>, type: ?string, code: string|int|null} $contents
*/
public function __construct(private readonly array $contents, public readonly ResponseInterface $response)
{
$this->statusCode = $response->getStatusCode();
$message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error';
if (is_array($message)) {
$message = implode(PHP_EOL, $message);
}
parent::__construct($message);
}
/**
* Returns the HTTP status code.
*
* **Note: For streamed requests it might be 200 even in case of an error.**
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* Returns the error message.
*/
public function getErrorMessage(): string
{
return $this->getMessage();
}
/**
* Returns the error type.
*/
public function getErrorType(): ?string
{
return $this->contents['type'];
}
/**
* Returns the error code.
*/
public function getErrorCode(): string|int|null
{
return $this->contents['code'];
}
}