/
UserMessageException.php
110 lines (97 loc) · 2.13 KB
/
UserMessageException.php
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Concrete\Core\Error;
use Concrete\Core\Error\ErrorList\Error\HtmlAwareErrorInterface;
use Exception;
use JsonSerializable;
/**
* Represents an error that can be safely shown to users.
*/
class UserMessageException extends Exception implements JsonSerializable, HtmlAwareErrorInterface
{
/**
* Can this exception be added to the log?
*
* @var bool
*/
protected $canBeLogged = false;
/**
* Does the message contain an HTML-formatted string?
*
* @since concrete5 8.5.0a3
*
* @var bool
*/
private $messageContainsHtml = false;
/**
* Can this exception be added to the log?
*
* @return bool
*/
public function canBeLogged()
{
return $this->canBeLogged;
}
/**
* Can this exception be added to the log?
*
* @param bool $value
*
* @return $this
*/
public function setCanBeLogged($value)
{
$this->canBeLogged = (bool) $value;
return $this;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Error\ErrorList\Error\HtmlAwareErrorInterface::messageContainsHtml()
* @since concrete5 8.5.0a3
*/
public function messageContainsHtml()
{
return $this->messageContainsHtml;
}
/**
* Does the message contain an HTML-formatted string?
*
* @param bool $value
*
* @return $this
*
* @since concrete5 8.5.0a3
*/
public function setMessageContainsHtml($value)
{
$this->messageContainsHtml = (bool) $value;
return $this;
}
/**
* {@inheritdoc}
*
* @see JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
{
$result = [
'error' => true,
'errors' => [
$this->getMessage(),
],
];
if ($this->messageContainsHtml()) {
$result['htmlErrorIndexes'] = [0];
}
return $result;
}
/**
* {@inheritdoc}
*
* @see \Exception::__toString()
*/
public function __toString()
{
return $this->getMessage();
}
}