|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2018 ChannelWeb Srl, Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under The MIT License |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace BEdita\SDK; |
|
14
|
|
|
|
|
15
|
|
|
use Exception; |
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Network exception thrown by BEdita API Client. |
|
20
|
|
|
*/ |
|
21
|
|
|
class BEditaClientException extends RuntimeException |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Array of attributes that are passed in from the constructor, and |
|
25
|
|
|
* made available in the view when a development error is displayed. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected array $attributes = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Template string that has attributes sprintf()'ed into it. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected string $messageTemplate = '[%s] %s'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Default exception code |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected int $defaultCode = 503; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* Allows you to create exceptions that are treated as framework errors and disabled |
|
49
|
|
|
* when debug = 0. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array|string $message Either the string of the error message, or an array of attributes |
|
52
|
|
|
* that are made available in the view, and sprintf()'d into Exception::$_messageTemplate |
|
53
|
|
|
* @param int|null $code The code of the error, is also the HTTP status code for the error. |
|
54
|
|
|
* @param \Exception|null $previous the previous exception. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(array|string $message = '', ?int $code = null, ?Exception $previous = null) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($code === null) { |
|
59
|
|
|
$code = $this->defaultCode; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (is_array($message)) { |
|
63
|
|
|
$this->attributes = $message; |
|
64
|
|
|
$message = vsprintf($this->messageTemplate, $message); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
parent::__construct($message, $code, $previous); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the passed in attributes |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getAttributes(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->attributes; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|