Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Response.php
Go to the documentation of this file.
1 <?php
9 
11 {
15  protected $_errorProcessor;
16 
20  protected $_renderer;
21 
25  protected $_appState;
26 
31  protected $exceptions = [];
32 
40  public function __construct(
41  \Magento\Framework\Webapi\Rest\Response\RendererFactory $rendererFactory,
42  \Magento\Framework\Webapi\ErrorProcessor $errorProcessor,
43  \Magento\Framework\App\State $appState
44  ) {
45  $this->_renderer = $rendererFactory->get();
46  $this->_errorProcessor = $errorProcessor;
47  $this->_appState = $appState;
48  }
49 
55  public function sendResponse()
56  {
57  try {
58  if ($this->isException()) {
59  $this->_renderMessages();
60  }
61  parent::sendResponse();
62  } catch (\Exception $e) {
63  if ($e instanceof \Magento\Framework\Webapi\Exception) {
64  // If the server does not support all MIME types accepted by the client it SHOULD send 406.
65  $httpCode = $e->getHttpCode() ==
69  } else {
71  }
72 
74  $this->_errorProcessor->renderException($e, $httpCode);
75  }
76  }
77 
83  protected function _renderMessages()
84  {
85  $responseHttpCode = null;
87  foreach ($this->getException() as $exception) {
88  $maskedException = $this->_errorProcessor->maskException($exception);
89  $messageData = [
90  'message' => $maskedException->getMessage(),
91  ];
92  if ($maskedException->getErrors()) {
93  $messageData['errors'] = [];
94  foreach ($maskedException->getErrors() as $errorMessage) {
95  $errorData['message'] = $errorMessage->getRawMessage();
96  $errorData['parameters'] = $errorMessage->getParameters();
97  $messageData['errors'][] = $errorData;
98  }
99  }
100  if ($maskedException->getCode()) {
101  $messageData['code'] = $maskedException->getCode();
102  }
103  if ($maskedException->getDetails()) {
104  $messageData['parameters'] = $maskedException->getDetails();
105  }
106  if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
107  $messageData['trace'] = $exception instanceof \Magento\Framework\Webapi\Exception
108  ? $exception->getStackTrace()
109  : $exception->getTraceAsString();
110  }
111  $responseHttpCode = $maskedException->getHttpCode();
112  }
113  // set HTTP code of the last error, Content-Type, and all rendered error messages to body
114  $this->setHttpResponseCode($responseHttpCode);
115  $this->setMimeType($this->_renderer->getMimeType());
116  $this->setBody($this->_renderer->render($messageData));
117  return $this;
118  }
119 
126  public function prepareResponse($outputData = null)
127  {
128  $this->_render($outputData);
129  if ($this->getMessages()) {
130  $this->_render(['messages' => $this->getMessages()]);
131  }
132  return $this;
133  }
134 
141  protected function _render($data)
142  {
143  $mimeType = $this->_renderer->getMimeType();
144  $body = $this->_renderer->render($data);
145  $this->setMimeType($mimeType)->setBody($body);
146  }
147 
154  public function setException($e)
155  {
156  $this->exceptions[] = $e;
157  return $this;
158  }
159 
165  public function isException()
166  {
167  return !empty($this->exceptions);
168  }
169 
175  public function getException()
176  {
177  return $this->exceptions;
178  }
179 
186  public function hasExceptionOfType($type)
187  {
188  foreach ($this->exceptions as $e) {
189  if ($e instanceof $type) {
190  return true;
191  }
192  }
193 
194  return false;
195  }
196 }
$type
Definition: item.phtml:13
__construct(\Magento\Framework\Webapi\Rest\Response\RendererFactory $rendererFactory, \Magento\Framework\Webapi\ErrorProcessor $errorProcessor, \Magento\Framework\App\State $appState)
Definition: Response.php:40