/**
  * Generate and set HTTP response code, error messages to Response object.
  *
  * @return $this
  */
 protected function _renderMessages()
 {
     $responseHttpCode = null;
     /** @var \Exception $exception */
     foreach ($this->getException() as $exception) {
         $maskedException = $this->_errorProcessor->maskException($exception);
         $messageData = ['message' => $maskedException->getMessage()];
         if ($maskedException->getErrors()) {
             $messageData['errors'] = [];
             foreach ($maskedException->getErrors() as $errorMessage) {
                 $errorData['message'] = $errorMessage->getRawMessage();
                 $errorData['parameters'] = $errorMessage->getParameters();
                 $messageData['errors'][] = $errorData;
             }
         }
         if ($maskedException->getCode()) {
             $messageData['code'] = $maskedException->getCode();
         }
         if ($maskedException->getDetails()) {
             $messageData['parameters'] = $maskedException->getDetails();
         }
         if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
             $messageData['trace'] = $exception instanceof \Magento\Framework\Webapi\Exception ? $exception->getStackTrace() : $exception->getTraceAsString();
         }
         $responseHttpCode = $maskedException->getHttpCode();
     }
     // set HTTP code of the last error, Content-Type, and all rendered error messages to body
     $this->setHttpResponseCode($responseHttpCode);
     $this->setMimeType($this->_renderer->getMimeType());
     $this->setBody($this->_renderer->render($messageData));
     return $this;
 }
 /**
  * Test sendResponse method with various exceptions
  *
  * @param \Exception $exception
  * @param int $expectedHttpCode
  * @param string $expectedMessage
  * @param array $expectedDetails
  * @return void
  * @dataProvider dataProviderForSendResponseExceptions
  */
 public function testMaskException($exception, $expectedHttpCode, $expectedMessage, $expectedDetails)
 {
     /** Assert that exception was logged. */
     // TODO:MAGETWO-21077 $this->_loggerMock->expects($this->once())->method('critical');
     $maskedException = $this->_errorProcessor->maskException($exception);
     $this->assertMaskedException($maskedException, $expectedHttpCode, $expectedMessage, $expectedDetails);
 }
    /**
     * Test handling exception during dispatch.
     */
    public function testDispatchWithException()
    {
        $exceptionMessage = 'some error message';
        $exception = new \Magento\Framework\Webapi\Exception(__($exceptionMessage));
        $this->_soapServerMock->expects($this->any())->method('handle')->will($this->throwException($exception));
        $this->_errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception));
        $encoding = "utf-8";
        $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding));
        $this->_soapController->dispatch($this->_requestMock);
        $expectedMessage = <<<EXPECTED_MESSAGE
<?xml version="1.0" encoding="{$encoding}"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" >
   <env:Body>
      <env:Fault>
         <env:Code>
            <env:Value>env:Sender</env:Value>
         </env:Code>
         <env:Reason>
            <env:Text xml:lang="en">some error message</env:Text>
         </env:Reason>
      </env:Fault>
   </env:Body>
</env:Envelope>
EXPECTED_MESSAGE;
        $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody());
    }
Exemple #4
0
 /**
  * Handle REST request
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return \Magento\Framework\App\ResponseInterface
  */
 public function dispatch(\Magento\Framework\App\RequestInterface $request)
 {
     $path = $this->_pathProcessor->process($request->getPathInfo());
     $this->_request->setPathInfo($path);
     $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE);
     try {
         $this->checkPermissions();
         $route = $this->getCurrentRoute();
         if ($route->isSecure() && !$this->_request->isSecure()) {
             throw new \Magento\Framework\Webapi\Exception(__('Operation allowed only in HTTPS'));
         }
         /** @var array $inputData */
         $inputData = $this->_request->getRequestData();
         $serviceMethodName = $route->getServiceMethod();
         $serviceClassName = $route->getServiceClass();
         $inputData = $this->paramsOverrider->override($inputData, $route->getParameters());
         $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
         $service = $this->_objectManager->get($serviceClassName);
         /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */
         $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams);
         $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName);
         if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) {
             $outputData = $this->fieldsFilter->filter($outputData);
         }
         $this->_response->prepareResponse($outputData);
     } catch (\Exception $e) {
         $maskedException = $this->_errorProcessor->maskException($e);
         $this->_response->setException($maskedException);
     }
     return $this->_response;
 }
 /**
  * Test logged exception is the same as the thrown one in production mode
  */
 public function testCriticalExceptionStackTrace()
 {
     $thrownException = new \Exception('', 0);
     $this->_loggerMock->expects($this->once())->method('critical')->will($this->returnCallback(function (\Exception $loggedException) use($thrownException) {
         $this->assertSame($thrownException, $loggedException->getPrevious());
     }));
     $this->_errorProcessor->maskException($thrownException);
 }
Exemple #6
0
 /**
  * Set body and status code to response using information extracted from provided exception.
  *
  * @param \Exception $exception
  * @return void
  */
 protected function _prepareErrorResponse($exception)
 {
     $maskedException = $this->_errorProcessor->maskException($exception);
     if ($this->_isWsdlRequest()) {
         $httpCode = $maskedException->getHttpCode();
         $contentType = self::CONTENT_TYPE_WSDL_REQUEST;
     } else {
         $httpCode = Response::HTTP_OK;
         $contentType = self::CONTENT_TYPE_SOAP_CALL;
     }
     $this->_setResponseContentType($contentType);
     $this->_response->setHttpResponseCode($httpCode);
     $soapFault = new \Magento\Webapi\Model\Soap\Fault($this->_request, $this->_soapServer, $maskedException, $this->_localeResolver, $this->_appState);
     $this->_setResponseBody($soapFault->toXml());
 }
Exemple #7
0
 /**
  * Handle REST request
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return \Magento\Framework\App\ResponseInterface
  */
 public function dispatch(\Magento\Framework\App\RequestInterface $request)
 {
     $path = $this->_pathProcessor->process($request->getPathInfo());
     $this->_request->setPathInfo($path);
     $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE);
     try {
         if ($this->isSchemaRequest()) {
             $this->processSchemaRequest();
         } else {
             $this->processApiRequest();
         }
     } catch (\Exception $e) {
         $maskedException = $this->_errorProcessor->maskException($e);
         $this->_response->setException($maskedException);
     }
     return $this->_response;
 }
 /**
  * Test sendResponse method with exception rendering.
  */
 public function testSendResponseWithException()
 {
     /** Mock all required objects. */
     $this->rendererMock->expects($this->any())->method('getMimeType')->will($this->returnValue('application/json'));
     $this->rendererMock->expects($this->any())->method('render')->will($this->returnCallback([$this, 'callbackForSendResponseTest'], $this->returnArgument(0)));
     $exceptionMessage = 'Message';
     $exceptionHttpCode = \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST;
     $exception = new \Magento\Framework\Webapi\Exception(new Phrase($exceptionMessage), 0, $exceptionHttpCode);
     $this->errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception));
     $this->responseRest->setException($exception);
     /** Start output buffering. */
     ob_start();
     $this->responseRest->sendResponse();
     /** Clear output buffering. */
     ob_end_clean();
     $actualResponse = $this->responseRest->getBody();
     $expectedResult = '{"message":"' . $exceptionMessage . '"}';
     $this->assertStringStartsWith($expectedResult, $actualResponse, 'Response body is invalid');
 }