Phprest\ErrorHandler\Formatter\JsonXml::format PHP Method

format() public method

public format ( Exception $exception ) : string
$exception Exception
return string
    public function format(\Exception $exception)
    {
        $response = new Response();
        try {
            $response = $this->serialize($this->config->isDebug() ? new Entity\DebugError($exception) : new Entity\Error($exception), is_null($this->request) ? Request::createFromGlobals() : $this->request, $response);
        } catch (\Exception $e) {
            $response->setContent($this->serviceHateoas()->getSerializer()->serialize($this->config->isDebug() ? new Entity\DebugError($e) : new Entity\Error($e), 'json'));
            $vendor = $this->getContainer()->get(Application::CONTAINER_ID_VENDOR);
            $apiVersion = $this->getContainer()->get(Application::CONTAINER_ID_API_VERSION);
            $response->headers->set('Content-Type', 'application/vnd.' . $vendor . '-v' . $apiVersion . '+json');
        }
        $response->setStatusCode(method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500);
        $response->sendHeaders();
        return $response->getContent();
    }

Usage Example

Beispiel #1
0
 public function testFormatWithNotAcceptable()
 {
     $request = Request::createFromGlobals();
     $request->headers->set('Accept', 'yaml');
     $jsonXmlFormatter = new JsonXml($this->config, $request);
     $this->assertContains('"code":0,"message":"Not Acceptable","details":["yaml is not supported"]', $jsonXmlFormatter->format(new \Exception()));
 }
All Usage Examples Of Phprest\ErrorHandler\Formatter\JsonXml::format