-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathRequestException.php
More file actions
88 lines (75 loc) · 2.06 KB
/
RequestException.php
File metadata and controls
88 lines (75 loc) · 2.06 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
<?php
declare(strict_types=1);
namespace Saloon\Exceptions\Request;
use Throwable;
use Saloon\Http\Response;
use Saloon\Http\PendingRequest;
use Saloon\Helpers\StatusCodeHelper;
use Saloon\Exceptions\SaloonException;
/**
* RequestException
*
* This exception is thrown when the response from a request is a failed response.
*
* @see https://docs.saloon.dev/the-basics/handling-failures
*/
class RequestException extends SaloonException
{
/**
* The Saloon Response
*
* @var Response<mixed>
*/
protected Response $response;
/**
* Maximum length allowed for the body
*/
protected int $maxBodyLength = 2_000;
/**
* Create the RequestException
*
* @param Response<mixed> $response
*/
public function __construct(Response $response, ?string $message = null, int $code = 0, ?Throwable $previous = null)
{
$this->response = $response;
if (is_null($message)) {
$status = $this->getStatus();
$statusCodeMessage = $this->getStatusMessage() ?? 'Unknown Status';
$rawBody = $response->body();
$exceptionBodyMessage = mb_strlen($rawBody) > $this->maxBodyLength ? mb_substr($rawBody, 0, $this->maxBodyLength) : $rawBody;
$message = sprintf('%s (%s) Response: %s', $statusCodeMessage, $status, $exceptionBodyMessage);
}
parent::__construct($message, $code, $previous);
}
/**
* Get the Saloon Response Class.
*
* @return Response<mixed>
*/
public function getResponse(): Response
{
return $this->response;
}
/**
* Get the pending request.
*/
public function getPendingRequest(): PendingRequest
{
return $this->getResponse()->getPendingRequest();
}
/**
* Get the HTTP status code
*/
public function getStatus(): int
{
return $this->response->status();
}
/**
* Get the status message
*/
public function getStatusMessage(): ?string
{
return StatusCodeHelper::getMessage($this->getStatus());
}
}