10 use \Magento\Framework\Webapi\ErrorProcessor;
34 $this->encoderMock = $this->getMockBuilder(\
Magento\Framework\
Json\Encoder::class)
35 ->disableOriginalConstructor()
36 ->setMethods([
'encode'])
39 $this->_appStateMock = $this->getMockBuilder(\
Magento\Framework\
App\State::class)
40 ->disableOriginalConstructor()
43 $this->_loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
45 $filesystemMock = $this->getMockBuilder(\
Magento\Framework\Filesystem::class)
46 ->disableOriginalConstructor()
62 unset($this->_errorProcessor);
63 unset($this->encoderMock);
64 unset($this->_appStateMock);
75 $_SERVER[
'HTTP_ACCEPT'] =
'json';
77 $this->encoderMock->expects(
82 $this->returnCallback([$this,
'callbackJsonEncode'], $this->returnArgument(0))
86 $this->_errorProcessor->renderErrorMessage(
'Message');
88 $actualResult = ob_get_contents();
90 $expectedResult =
'{"messages":{"error":[{"code":500,"message":"Message"}]}}';
91 $this->assertEquals($expectedResult, $actualResult,
'Invalid rendering in JSON.');
104 return json_encode(
$data);
113 $_SERVER[
'HTTP_ACCEPT'] =
'json';
115 $this->_appStateMock->expects($this->any())->method(
'getMode')->will($this->returnValue(
'developer'));
117 $this->encoderMock->expects(
122 $this->returnCallback([$this,
'callbackJsonEncode'], $this->returnArgument(0))
125 $this->_errorProcessor->renderErrorMessage(
'Message',
'Message trace.', 401);
126 $actualResult = ob_get_contents();
128 $expectedResult =
'{"messages":{"error":[{"code":401,"message":"Message","trace":"Message trace."}]}}';
129 $this->assertEquals($expectedResult, $actualResult,
'Invalid rendering in JSON.');
138 $_SERVER[
'HTTP_ACCEPT'] =
'xml';
141 $this->_errorProcessor->renderErrorMessage(
'Message');
143 $actualResult = ob_get_contents();
145 $expectedResult =
'<?xml version="1.0"?><error><messages><error><data_item><code>500</code>' .
146 '<message><![CDATA[Message]]></message></data_item></error></messages></error>';
147 $this->assertEquals($expectedResult, $actualResult,
'Invalid rendering in XML.');
156 $_SERVER[
'HTTP_ACCEPT'] =
'xml';
158 $this->_appStateMock->expects($this->any())->method(
'getMode')->will($this->returnValue(
'developer'));
161 $this->_errorProcessor->renderErrorMessage(
'Message',
'Trace message.', 401);
163 $actualResult = ob_get_contents();
165 $expectedResult =
'<?xml version="1.0"?><error><messages><error><data_item><code>401</code><message>' .
166 '<![CDATA[Message]]></message><trace><![CDATA[Trace message.]]></trace></data_item></error>' .
167 '</messages></error>';
168 $this->assertEquals($expectedResult, $actualResult,
'Invalid rendering in XML with turned on developer mode.');
178 $_SERVER[
'HTTP_ACCEPT'] =
'undefined';
180 $this->encoderMock->expects($this->atLeastOnce())->method(
'encode');
181 $this->_errorProcessor->renderErrorMessage(
'Message');
191 $this->_appStateMock->expects($this->once())->method(
'getMode')->will($this->returnValue(
'developer'));
193 $errorMessage =
'Error Message';
194 $logicalException = new \LogicException($errorMessage);
196 $maskedException = $this->_errorProcessor->maskException($logicalException);
197 $this->assertInstanceOf(\
Magento\Framework\Webapi\Exception::class, $maskedException);
200 $maskedException->getMessage(),
201 'Exception was masked incorrectly in developer mode.' 215 public function testMaskException($exception, $expectedHttpCode, $expectedMessage, $expectedDetails)
219 $maskedException = $this->_errorProcessor->maskException($exception);
220 $this->assertMaskedException(
233 $thrownException = new \Exception(
'', 0);
235 $this->_loggerMock->expects($this->once())
238 $this->returnCallback(
239 function (\
Exception $loggedException) use ($thrownException) {
240 $this->assertSame($thrownException, $loggedException->getPrevious());
244 $this->_errorProcessor->maskException($thrownException);
253 'NoSuchEntityException' => [
256 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
258 'fieldName' =>
'detail1',
259 'fieldValue' =>
'value1',
260 'field2Name' =>
'resource_id',
261 'field2Value' =>
'resource10',
266 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
268 'fieldName' =>
'detail1',
269 'fieldValue' =>
'value1',
270 'field2Name' =>
'resource_id',
271 'field2Value' =>
'resource10',
274 'NoSuchEntityException (Empty message)' => [
276 WebapiException::HTTP_NOT_FOUND,
280 'AuthorizationException' => [
283 'Consumer %consumer_id is not authorized to access %resources',
284 [
'consumer_id' =>
'3',
'resources' =>
'4']
287 WebapiException::HTTP_UNAUTHORIZED,
288 'Consumer %consumer_id is not authorized to access %resources',
289 [
'consumer_id' =>
'3',
'resources' =>
'4'],
292 new \Exception(
'Non service exception', 5678),
293 WebapiException::HTTP_INTERNAL_ERROR,
294 'Internal Error. Details are available in Magento log file. Report ID:',
309 public function assertMaskedException(
316 $expectedType = \Magento\Framework\Webapi\Exception::class;
317 $this->assertInstanceOf(
320 "Masked exception type is invalid: expected '{$expectedType}', given '" . get_class(
327 $maskedException->getHttpCode(),
328 "Masked exception HTTP code is invalid: expected '{$expectedHttpCode}', " .
329 "given '{$maskedException->getHttpCode()}'." 331 $this->assertContains(
333 $maskedException->getMessage(),
334 "Masked exception message is invalid: expected '{$expectedMessage}', " .
335 "given '{$maskedException->getMessage()}'." 337 $this->assertEquals($expectedDetails, $maskedException->getDetails(),
"Masked exception details are invalid.");
testRenderXmlInDeveloperMode()
testMaskException($exception, $expectedHttpCode, $expectedMessage, $expectedDetails)
dataProviderForSendResponseExceptions()
callbackJsonEncode($data)
testRenderJsonInDeveloperMode()
testMaskExceptionInDeveloperMode()
testRenderDefaultFormat()
testCriticalExceptionStackTrace()