|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PedroQuezado\Code\Braspress; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Exception class for handling errors specific to the Braspress client. |
|
9
|
|
|
*/ |
|
10
|
|
|
class BraspressClienteException extends Exception |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var int|null The HTTP status code associated with the exception, if applicable. |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $httpCode; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var mixed|null The response body associated with the exception, if applicable. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $response; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* BraspressClienteException constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $message The exception message. |
|
26
|
|
|
* @param int|null $httpCode The HTTP status code, if applicable. |
|
27
|
|
|
* @param mixed|null $response The response body, if applicable. |
|
28
|
|
|
* @param int $code The internal exception code. |
|
29
|
|
|
* @param Exception|null $previous The previous exception used for exception chaining. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($message = "", $httpCode = null, $response = null, $code = 0, Exception $previous = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->httpCode = $httpCode; |
|
34
|
|
|
$this->response = $response; |
|
35
|
|
|
parent::__construct($message, $code, $previous); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the HTTP status code associated with the exception. |
|
40
|
|
|
* |
|
41
|
|
|
* @return int|null The HTTP status code, or null if not set. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getHttpCode() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->httpCode; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the response body associated with the exception. |
|
50
|
|
|
* |
|
51
|
|
|
* @return mixed|null The response body, or null if not set. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getResponse() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->response; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Convert the exception to a string representation. |
|
60
|
|
|
* |
|
61
|
|
|
* This method provides a detailed string that includes the class name, message, |
|
62
|
|
|
* HTTP code, and response body (if available). |
|
63
|
|
|
* |
|
64
|
|
|
* @return string The string representation of the exception. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __toString() |
|
67
|
|
|
{ |
|
68
|
|
|
$responseDetails = is_array($this->response) ? json_encode($this->response) : $this->response; |
|
69
|
|
|
return __CLASS__ . ": [{$this->code}]: {$this->message}\nHTTP Code: {$this->httpCode}\nResponse: {$responseDetails}\n"; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|