Example #1
0
 /**
  * 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);
 }
Example #3
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);
 }
Example #5
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());
 }
Example #6
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;
 }