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')]);
    }