private function __prepareResponse($exception, $options = [])
    {
        $response = $this->_getController()->response;
        $code = $this->_code($exception);
        $response->getStatusCode($this->_code($exception));

        Configure::write('apiExceptionMessage', $exception->getMessage());

        $responseFormat = $this->_getController()->responseFormat;
        $responseData = [
            $responseFormat['statusKey'] => !empty($options['responseStatus']) ? $options['responseStatus'] : $responseFormat['statusNokText'],
            $responseFormat['resultKey'] => [
                $responseFormat['errorKey'] => ($code < 500) ? 'Not Found' : 'An Internal Error Has Occurred.',
            ],
        ];

        if ((isset($options['customMessage']) && $options['customMessage']) || Configure::read('ApiRequest.debug')) {
            $responseData[$responseFormat['resultKey']][$responseFormat['errorKey']] = $exception->getMessage();
        }

        if ('xml' === Configure::read('ApiRequest.responseType')) {
            $body = $response->getBody();
            $body->write(Xml::fromArray([Configure::read('ApiRequest.xmlResponseRootNode') => $responseData], 'tags')->asXML());
            $response->withBody($body);
        } else {
            $body = $response->getBody();
            $body->write(json_encode($responseData));
            $response->withBody($body);
        }

        $this->controller->response = $response;

        return $response;
    }
private function __prepareResponse($exception, $options = [])
    {
        $response = $this->_getController()->response;
        $code = $this->_code($exception);
        $response->getStatusCode($this->_code($exception));

        Configure::write('apiExceptionMessage', $exception->getMessage());

        $responseFormat = $this->_getController()->responseFormat;
        $responseData = [
            $responseFormat['statusKey'] => !empty($options['responseStatus']) ? $options['responseStatus'] : $responseFormat['statusNokText'],
            $responseFormat['resultKey'] => [
                $responseFormat['errorKey'] => ($code < 500) ? 'Not Found' : 'An Internal Error Has Occurred.',
            ],
        ];

        if ((isset($options['customMessage']) && $options['customMessage']) || Configure::read('ApiRequest.debug')) {
            $responseData[$responseFormat['resultKey']][$responseFormat['errorKey']] = $exception->getMessage();
        }

        if ('xml' === Configure::read('ApiRequest.responseType')) {
            $body = $response->getBody();
            $body->write(Xml::fromArray([Configure::read('ApiRequest.xmlResponseRootNode') => $responseData], 'tags')->asXML());
            $response->withBody($body);
        } else {
            $body = $response->getBody();
            $body->write(json_encode($responseData));
            $response->withBody($body);
        }

        $this->controller->response = $response;

        return $response;
    }
private function __prepareResponse ( $ exception , $ options = [ ] ) { $ response = $ this -> _getController ( ) -> response ; $ code = $ this -> _code ( $ exception ) ; $ response -> getStatusCode ( $ this -> _code ( $ exception ) ) ; Configure :: write ( 'apiExceptionMessage' , $ exception -> getMessage ( ) ) ; $ responseFormat = $ this -> _getController ( ) -> responseFormat ; $ responseData = [ $ responseFormat [ 'statusKey' ] => ! empty ( $ options [ 'responseStatus' ] ) ? $ options [ 'responseStatus' ] : $ responseFormat [ 'statusNokText' ] , $ responseFormat [ 'resultKey' ] => [ $ responseFormat [ 'errorKey' ] => ( $ code < 500 ) ? 'Not Found' : 'An Internal Error Has Occurred.' , ] , ] ; if ( ( isset ( $ options [ 'customMessage' ] ) && $ options [ 'customMessage' ] ) || Configure :: read ( 'ApiRequest.debug' ) ) { $ responseData [ $ responseFormat [ 'resultKey' ] ] [ $ responseFormat [ 'errorKey' ] ] = $ exception -> getMessage ( ) ; } if ( 'xml' === Configure :: read ( 'ApiRequest.responseType' ) ) { $ body = $ response -> getBody ( ) ; $ body -> write ( Xml :: fromArray ( [ Configure :: read ( 'ApiRequest.xmlResponseRootNode' ) => $ responseData ] , 'tags' ) -> asXML ( ) ) ; $ response -> withBody ( $ body ) ; } else { $ body = $ response -> getBody ( ) ; $ body -> write ( json_encode ( $ responseData ) ) ; $ response -> withBody ( $ body ) ; } $ this -> controller -> response = $ response ; return $ response ; }
Prepare response.

@param Exception $exception Exception
@param array     $options   Array of options

@return Response
Prepare response .