Пример #1
0
 /**
  * Common function to handle exceptions in the data service.
  * 
  * @param \Exception    $exception    exception
  * @param IService      $service service
  * 
  * @return void
  */
 public static function handleException($exception, IService $service)
 {
     $acceptTypesText = $service->getHost()->getRequestAccept();
     $responseContentType = null;
     try {
         $responseContentType = HttpProcessUtility::selectMimeType($acceptTypesText, array(MimeTypes::MIME_APPLICATION_XML, MimeTypes::MIME_APPLICATION_JSON));
     } catch (HttpHeaderFailure $exception) {
         $exception = new ODataException($exception->getMessage(), $exception->getStatusCode());
     } catch (\Exception $exception) {
         // Never come here
     }
     if (is_null($responseContentType)) {
         $responseContentType = MimeTypes::MIME_APPLICATION_XML;
     }
     if (!$exception instanceof ODataException) {
         $exception = new ODataException($exception->getMessage(), HttpStatus::CODE_INTERNAL_SERVER_ERROR);
     }
     $service->getHost()->setResponseVersion(ODataConstants::DATASERVICEVERSION_1_DOT_0 . ';');
     // At this point all kind of exceptions will be converted
     //to 'ODataException'
     if ($exception->getStatusCode() == HttpStatus::CODE_NOT_MODIFIED) {
         $service->getHost()->setResponseStatusCode(HttpStatus::CODE_NOT_MODIFIED);
     } else {
         $service->getHost()->setResponseStatusCode($exception->getStatusCode());
         $service->getHost()->setResponseContentType($responseContentType);
         $responseBody = null;
         if (strcasecmp($responseContentType, MimeTypes::MIME_APPLICATION_XML) == 0) {
             $responseBody = AtomODataWriter::serializeException($exception, true);
         } else {
             $responseBody = JsonODataV2Writer::serializeException($exception, true);
         }
         $service->getHost()->getOperationContext()->outgoingResponse()->setStream($responseBody);
     }
 }
Пример #2
0
 /**
  * Serialize the exception
  *
  * @param ODataException $exception              Exception to serialize
  * @param boolean        $serializeInnerException if set to true,
  * serialize the inner exception if $exception is an ODataException.
  * 
  * @return string
  */
 public static function serializeException(ODataException $exception, $serializeInnerException)
 {
     $xmlWriter = new \XMLWriter();
     $xmlWriter->openMemory();
     $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
     $xmlWriter->setIndent(4);
     $xmlWriter->startElement(ODataConstants::XML_ERROR_ELEMENT_NAME);
     //$xmlWriter->writeAttributeNs(
     //    ODataConstants::XMLNS_NAMESPACE_PREFIX,
     //    ODataConstants::XML_NAMESPACE_PREFIX,
     //    ODataConstants::XML_NAMESPACE,
     //    null
     //);
     $xmlWriter->writeAttribute(ODataConstants::XMLNS_NAMESPACE_PREFIX, ODataConstants::ODATA_METADATA_NAMESPACE);
     $xmlWriter->endAttribute();
     $xmlWriter->startElement(ODataConstants::XML_ERROR_CODE_ELEMENT_NAME);
     if ($exception->getCode() != null) {
         $xmlWriter->text($exception->getCode());
     }
     $xmlWriter->endElement();
     $xmlWriter->startElement(ODataConstants::XML_ERROR_MESSAGE_ELEMENT_NAME);
     $xmlWriter->text($exception->getMessage());
     $xmlWriter->endElement();
     $xmlWriter->endElement();
     $xmlWriter->endDocument();
     return $xmlWriter->outputMemory(true);
 }
Пример #3
0
 /**
  * serialize exception.
  *
  * @param ODataException $exception              Exception to serialize
  * @param Boolean        $serializeInnerException if set to true
  *
  * serialize the inner exception if $exception is an ODataException.
  *
  * @return string
  */
 public static function serializeException(ODataException $exception, $serializeInnerException)
 {
     $writer = new JsonWriter('');
     // Wrapper for error.
     $writer->startObjectScope()->writeName(ODataConstants::JSON_ERROR)->startObjectScope();
     // "code"
     if ($exception->getCode() != null) {
         $writer->writeName(ODataConstants::JSON_ERROR_CODE)->writeValue($exception->getCode());
     }
     // "message"
     $writer->writeName(ODataConstants::JSON_ERROR_MESSAGE)->startObjectScope()->writeName(ODataConstants::XML_LANG_ATTRIBUTE_NAME)->writeValue('en-US')->writeName(ODataConstants::JSON_ERROR_VALUE)->writeValue($exception->getMessage())->endScope()->endScope()->endScope();
     return $writer->getJsonOutput();
 }