Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
8
9 use eZ\Publish\Core\Base\Translatable;
10 use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor;
11 use eZ\Publish\Core\REST\Common\Output\Generator;
12 use eZ\Publish\Core\REST\Common\Output\Visitor;
13 use Symfony\Component\Translation\TranslatorInterface;
14
18 class Exception extends ValueObjectVisitor
19 {
25 protected $debug = false;
26
32 protected $httpStatusCodes = [
33 400 => 'Bad Request',
34 401 => 'Unauthorized',
35 402 => 'Payment Required',
36 403 => 'Forbidden',
37 404 => 'Not Found',
38 405 => 'Method Not Allowed',
39 406 => 'Not Acceptable',
40 407 => 'Proxy Authentication Required',
41 408 => 'Request Time-out',
42 409 => 'Conflict',
43 410 => 'Gone',
44 411 => 'Length Required',
45 412 => 'Precondition Failed',
46 413 => 'Request Entity Too Large',
47 414 => 'Request-URI Too Long',
48 415 => 'Unsupported Media Type',
49 416 => 'Requested range not satisfiable',
50 417 => 'Expectation Failed',
51 418 => "I'm a teapot",
52 421 => 'There are too many connections from your internet address',
53 422 => 'Unprocessable Entity',
54 423 => 'Locked',
55 424 => 'Failed Dependency',
56 425 => 'Unordered Collection',
57 426 => 'Upgrade Required',
58 500 => 'Internal Server Error',
59 501 => 'Not Implemented',
60 502 => 'Bad Gateway',
61 503 => 'Service Unavailable',
62 504 => 'Gateway Time-out',
63 505 => 'HTTP Version not supported',
64 506 => 'Variant Also Negotiates',
65 507 => 'Insufficient Storage',
66 509 => 'Bandwidth Limit Exceeded',
67 510 => 'Not Extended',
68 ];
69
71 protected $translator;
72
79 public function __construct($debug = false, TranslatorInterface $translator = null)
80 {
81 $this->debug = (bool)$debug;
82 $this->translator = $translator;
83 }
84
90 protected function getStatus()
91 {
92 return 500;
93 }
94
102 public function visit(Visitor $visitor, Generator $generator, $data)
103 {
104 $generator->startObjectElement('ErrorMessage');
105
106 $statusCode = $this->getStatus();
107 $visitor->setStatus($statusCode);
108 $visitor->setHeader('Content-Type', $generator->getMediaType('ErrorMessage'));
109
110 $generator->startValueElement('errorCode', $statusCode);
111 $generator->endValueElement('errorCode');
112
113 $generator->startValueElement('errorMessage', $this->httpStatusCodes[$statusCode]);
114 $generator->endValueElement('errorMessage');
115
116 if ($this->debug || $statusCode < 500) {
117 $errorDescription = $data instanceof Translatable && $this->translator
118 ? $this->translator->trans($data->getMessageTemplate(), $data->getParameters(), 'repository_exceptions')
119 : $data->getMessage();
120 } else {
121 // Do not leak any file paths and sensitive data on production environments
122 $errorDescription = $this->translator
123 ? $this->translator->trans('non_verbose_error', [], 'repository_exceptions')
124 : 'An error has occurred. Please try again later or contact your Administrator.';
125 }
126
127 $generator->startValueElement('errorDescription', $errorDescription);
128 $generator->endValueElement('errorDescription');
129
130 if ($this->debug) {
131 $generator->startValueElement('trace', $data->getTraceAsString());
132 $generator->endValueElement('trace');
133
134 $generator->startValueElement('file', $data->getFile());
135 $generator->endValueElement('file');
136
137 $generator->startValueElement('line', $data->getLine());
138 $generator->endValueElement('line');
139 }
140
141 if ($previous = $data->getPrevious()) {
142 $generator->startObjectElement('Previous', 'ErrorMessage');
143 $visitor->visitValueObject($previous);
144 $generator->endObjectElement('Previous');
145 }
146
147 $generator->endObjectElement('ErrorMessage');
148 }
149 }
eZ\Publish\Core\REST\Common\Output\Generator
Output generator.
Definition: REST/Common/Output/Generator.php:13
eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor
Basic ValueObjectVisitor.
Definition: ValueObjectVisitor.php:18
eZ\Publish\Core\REST\Common\Output\Visitor
Visits a value object into an HTTP Response.
Definition: Visitor.php:15
eZ\Publish\Core\REST\Common\Output\Visitor\visitValueObject
visitValueObject($data)
Visit struct returned by controllers.
Definition: Visitor.php:131
eZ\Publish\Core\REST\Common\Output\Visitor\setStatus
setStatus($statusCode)
Sets the given status code in the corresponding header.
Definition: Visitor.php:78
eZ\Publish\Core\REST\Common\Output\Visitor\setHeader
setHeader($name, $value)
Set HTTP response header.
Definition: Visitor.php:64
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception
Exception value object visitor.
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:19
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception\$httpStatusCodes
$httpStatusCodes
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:32
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception\visit
visit(Visitor $visitor, Generator $generator, $data)
Visit struct returned by controllers.
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:102
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception\$debug
$debug
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:25
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception\getStatus
getStatus()
Returns HTTP status code.
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:90
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception\$translator
$translator
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:71
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception\__construct
__construct($debug=false, TranslatorInterface $translator=null)
Construct from debug flag.
Definition: Publish/Core/REST/Server/Output/ValueObjectVisitor/Exception.php:79
eZ\Publish\Core\Base\Translatable
Interface for translatable value objects.
Definition: Core/Base/Translatable.php:13
eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor
Definition: Common/Output/ValueObjectVisitor/ContentObjectStates.php:7
eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor
Definition: Output/ValueObjectVisitor/BadRequestException.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.