src/Sds/ExceptionModule/JsonExceptionStrategy.php
public function prepareExceptionViewModel(MvcEvent $e)
    {
        // Do nothing if no error in the event
        if ( ! ($error = $e->getError())) {
            return;
        }

        // Do nothing if the result is a response object
        $result = $e->getResult();
        if ($result instanceof Response) {
            return;
        }

        if ($error != Application::ERROR_EXCEPTION){
            return;
        }

        if (! $e->getRequest() instanceof Request){
            return;
        }

        $accept = $e->getRequest()->getHeaders()->get('Accept');
        if (! ($accept && $accept->match('application/json'))){
            return;
        }

        if (! ($exception = $e->getParam('exception'))){
            return;
        }

        $modelData = $this->serializeException($exception);
        $e->setResult(new JsonModel($modelData));
        $e->setError(false);

        $response = $e->getResponse();
        if (!$response) {
            $response = new HttpResponse();
            $e->setResponse($response);
        }

        if (isset($modelData['statusCode'])){
            $response->setStatusCode($modelData['statusCode']);
        } else {
            $response->setStatusCode(500);
        }
        $response->getHeaders()->addHeaders([$accept, ContentType::fromString('Content-type: application/api-problem+json')]);
    }
public function prepareExceptionViewModel(MvcEvent $e)
    {
        // Do nothing if no error in the event
        if ( ! ($error = $e->getError())) {
            return;
        }

        // Do nothing if the result is a response object
        $result = $e->getResult();
        if ($result instanceof Response) {
            return;
        }

        if ($error != Application::ERROR_EXCEPTION){
            return;
        }

        if (! $e->getRequest() instanceof Request){
            return;
        }

        $accept = $e->getRequest()->getHeaders()->get('Accept');
        if (! ($accept && $accept->match('application/json'))){
            return;
        }

        if (! ($exception = $e->getParam('exception'))){
            return;
        }

        $modelData = $this->serializeException($exception);
        $e->setResult(new JsonModel($modelData));
        $e->setError(false);

        $response = $e->getResponse();
        if (!$response) {
            $response = new HttpResponse();
            $e->setResponse($response);
        }

        if (isset($modelData['statusCode'])){
            $response->setStatusCode($modelData['statusCode']);
        } else {
            $response->setStatusCode(500);
        }
        $response->getHeaders()->addHeaders([$accept, ContentType::fromString('Content-type: application/api-problem+json')]);
    }
public function prepareExceptionViewModel ( MvcEvent $ e ) { // Do nothing if no error in the event if ( ! ( $ error = $ e -> getError ( ) ) ) { return ; } // Do nothing if the result is a response object $ result = $ e -> getResult ( ) ; if ( $ result instanceof Response ) { return ; } if ( $ error != Application :: ERROR_EXCEPTION ) { return ; } if ( ! $ e -> getRequest ( ) instanceof Request ) { return ; } $ accept = $ e -> getRequest ( ) -> getHeaders ( ) -> get ( 'Accept' ) ; if ( ! ( $ accept && $ accept -> match ( 'application/json' ) ) ) { return ; } if ( ! ( $ exception = $ e -> getParam ( 'exception' ) ) ) { return ; } $ modelData = $ this -> serializeException ( $ exception ) ; $ e -> setResult ( new JsonModel ( $ modelData ) ) ; $ e -> setError ( false ) ; $ response = $ e -> getResponse ( ) ; if ( ! $ response ) { $ response = new HttpResponse ( ) ; $ e -> setResponse ( $ response ) ; } if ( isset ( $ modelData [ 'statusCode' ] ) ) { $ response -> setStatusCode ( $ modelData [ 'statusCode' ] ) ; } else { $ response -> setStatusCode ( 500 ) ; } $ response -> getHeaders ( ) -> addHeaders ( [ $ accept , ContentType :: fromString ( 'Content-type: application/api-problem+json' ) ] ) ; }
Create an exception json view model, and set the HTTP status code

@todo   dispatch.error does not halt dispatch unless a response is
returned. As such, we likely need to trigger rendering as a low
priority dispatch.error event (or goto a render event) to ensure
rendering occurs, and that munging of view models occurs when
expected.
@param  MvcEvent $e
@return void
Create an exception json view model and set the HTTP status code