1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2014 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Garden\Exception; |
9
|
|
|
|
10
|
|
|
use Garden\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Represents a 400 series exception. |
14
|
|
|
*/ |
15
|
|
|
class ClientException extends \Exception implements \JsonSerializable { |
16
|
|
|
protected $context; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Initialize an instance of the {@link ClientException} class. |
20
|
|
|
* |
21
|
|
|
* The 4xx class of status code is intended for cases in which the client seems to have erred. |
22
|
|
|
* When constructing a client exception you can pass additional information on the {@link $context} parameter |
23
|
|
|
* to aid in rendering. |
24
|
|
|
* |
25
|
|
|
* - Keys beginning with **HTTP_** will be added as headers. |
26
|
|
|
* - **description** will give the exception a longer description. |
27
|
|
|
* |
28
|
|
|
* @param string $message The error message. |
29
|
|
|
* @param int $code The http error code. |
30
|
|
|
* @param array $context An array of context variables that can be used to render a more detailed response. |
31
|
|
|
*/ |
32
|
23 |
|
public function __construct($message = '', $code = 400, array $context = []) { |
33
|
23 |
|
parent::__construct($message, $code); |
34
|
23 |
|
$this->context = $context; |
35
|
23 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets the response headers that were set on the exception. |
39
|
|
|
* |
40
|
|
|
* @return array Returns the headers for this exception. |
41
|
|
|
*/ |
42
|
23 |
|
public function getHeaders() { |
43
|
23 |
|
$result = []; |
44
|
23 |
|
foreach ($this->context as $key => $value) { |
45
|
23 |
|
if (stripos($key, 'http_') === 0) { |
46
|
7 |
|
$key = Response::normalizeHeader(ltrim_substr($key, 'http_')); |
47
|
7 |
|
$result[$key] = $value; |
48
|
7 |
|
} |
49
|
23 |
|
} |
50
|
23 |
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets a longer description for the exception. |
55
|
|
|
* |
56
|
|
|
* @return string Returns the description of the exception or an empty string if there isn't one. |
57
|
|
|
*/ |
58
|
23 |
|
public function getDescription() { |
59
|
23 |
|
return val('description', $this->context, ''); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Specify data which should be serialized to JSON. |
64
|
|
|
* |
65
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
66
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
67
|
|
|
* which is a value of any type other than a resource. |
68
|
|
|
*/ |
69
|
23 |
|
public function jsonSerialize() { |
70
|
|
|
$result = [ |
71
|
23 |
|
'message' => $this->getMessage(), |
72
|
23 |
|
'status' => $this->getCode() |
73
|
23 |
|
]; |
74
|
23 |
|
if ($this->getDescription()) { |
75
|
4 |
|
$result['description'] = $this->getDescription(); |
76
|
4 |
|
} |
77
|
23 |
|
return $result; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|