Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     $documentBuilder = $this->documentBuilderFactory->createDocumentBuilder();
     try {
         if ($context->hasErrors()) {
             $documentBuilder->setErrorCollection($context->getErrors());
             // remove errors from the Context to avoid processing them by other processors
             $context->resetErrors();
         } elseif ($context->hasResult()) {
             $result = $context->getResult();
             if (empty($result)) {
                 $documentBuilder->setDataCollection($result);
             } else {
                 $documentBuilder->setDataCollection($result, $context->getMetadata());
             }
         }
         $context->setResult($documentBuilder->getDocument());
     } catch (\Exception $e) {
         $context->setResponseStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
         $error = new Error();
         $error->setInnerException($e);
         $documentBuilder = $this->documentBuilderFactory->createDocumentBuilder();
         $documentBuilder->setErrorObject($error);
         $context->setResult($documentBuilder->getDocument());
     }
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 protected function executeProcessors(ContextInterface $context)
 {
     /** @var Context $context */
     $processors = $this->processorBag->getProcessors($context);
     try {
         /** @var ProcessorInterface $processor */
         foreach ($processors as $processor) {
             $processor->process($context);
         }
     } catch (\Exception $e) {
         // throw an exception was raised in normalize_result group as is
         // to avoid circular handling of such exception
         if (self::NORMALIZE_RESULT_GROUP === $processors->getGroup()) {
             throw $e;
         }
         // add an error to the context
         $error = new Error();
         $error->setInnerException($e);
         $context->addError($error);
         // go to the 'normalize_result' group that is intended
         // to prepare valid response of the current request type
         $context->setFirstGroup(self::NORMALIZE_RESULT_GROUP);
         $this->executeNormalizeResultProcessors($context);
     }
 }
 /**
  * @param Error $error
  *
  * @return array
  */
 protected function handleError(Error $error)
 {
     $result = [];
     if ($error->getStatusCode()) {
         $result['code'] = (string) $error->getStatusCode();
     }
     if ($error->getDetail()) {
         $result['detail'] = $error->getDetail();
     }
     if ($error->getTitle()) {
         $result['title'] = $error->getTitle();
     }
     return $result;
 }
 /**
  * @param int|null $statusCode
  *
  * @return Error
  */
 protected function getError($statusCode = null)
 {
     $error = new Error();
     $error->setStatusCode($statusCode);
     return $error;
 }