Get the error response associated with the given exception.

@param \Exception $exception
@param string     $id
@param int        $code
@param string[]   $headers

@return \Symfony\Component\HttpFoundation\Response
330504
public function display(Exception $exception, string $id, int $code, array $headers)
    {
        $info = $this->info->generate($exception, $id, 400);

        $error = ['id' => $id, 'status' => $info['code'], 'title' => $info['name'], 'detail' => $info['detail'], 'meta' => ['details' => $exception->getMessageBag()->all()]];

        return new JsonResponse(['errors' => [$error]], 400, array_merge($headers, ['Content-Type' => $this->contentType()]));
    }
1309660
protected function _template(Exception $exception, $method, $code)
    {
        $exception = $this->_unwrap($exception);
        $isHttpException = $exception instanceof HttpException;

        if (!Configure::read('debug') && !$isHttpException || $isHttpException) {
            $template = 'error500';
            if ($code < 500) {
                $template = 'error400';
            }

            return $this->template = $template;
        }

        $template = $method ?: 'error500';

        if ($exception instanceof PDOException) {
            $template = 'pdo_error';
        }

        return $this->template = $template;
    }
1326867
public static function createExceptionForResponse(
        array $allMessage,
        int $responseCode,
        string $responseId
    ): ApiException {
        $errorMessage = static::generateErrorMessage($responseCode, $allMessage, $responseId);

        switch ($responseCode) {
            case self::HTTP_RESPONSE_CODE_BAD_REQUEST:
                return new BadRequestException($errorMessage, $responseCode, $responseId);
            case self::HTTP_RESPONSE_CODE_UNAUTHORIZED:
                return new UnauthorizedException($errorMessage, $responseCode, $responseId);
            case self::HTTP_RESPONSE_CODE_FORBIDDEN:
                return new ForbiddenException($errorMessage, $responseCode, $responseId);
            case self::HTTP_RESPONSE_CODE_NOT_FOUND:
                return new NotFoundException($errorMessage, $responseCode, $responseId);
            case self::HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED:
                return new MethodNotAllowedException($errorMessage, $responseCode, $responseId);
            case self::HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS:
                return new TooManyRequestsException($errorMessage, $responseCode, $responseId);
            case self::HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR:
                return new PleaseContactBunqException($errorMessage, $responseCode, $responseId);
            default:
                return new UnknownApiErrorException($errorMessage, $responseCode, $responseId);
        }
    }
972104
public function render($request, Exception $exception)
    {
        $exception = $this->prepareException($exception);

        if ($exception instanceof HttpResponseException) {
            return $exception->getResponse();
        } elseif ($exception instanceof AuthenticationException) {
            return $this->unauthenticated($request, $exception);
        } elseif ($exception instanceof ValidationException) {
            return $this->convertValidationExceptionToResponse($exception, $request);
        } elseif ($this->isHttpException($exception) && $this->customErrorHtmlTemplateExists($exception)) {
            return $this->prepareResponse($request, $exception);
        }

        $flattened = FlattenException::create($exception);

        $code = $flattened->getStatusCode();
        $headers = $flattened->getHeaders();
        $response = $this->getContent($exception, $code, $request);

        // If it's already a response, return that.
        if (!$response instanceof BaseResponse) {
            $response = new Response($this->getContent($exception, $code, $request), $code, $headers);
        }

        return $response;
    }
843227
public function display( Exception $exception, $id, $code, array $headers )
	{
		$info = $this->info->generate( $exception, $id, $code );

		return new Response( $this->render( $info ), $code, array_merge( $headers, ['Content-Type' => $this->contentType()] ) );
	}
1052188
public function canDisplay(Exception $original, Exception $transformed, int $code)
    {
        $redirect = $transformed instanceof HttpExceptionInterface && $transformed->getStatusCode() === 401;

        return $redirect && !$this->request->is('api*');
    }
1369264
public function error(array $data = [], $status = 400, $code = 'error', array $headers = [], $options = 0)
    {
        return $this->respond($data, $status, $code, $headers, $options);
    }
452400
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;
    }
518752
public static function getException($transactionId, $responseCode, $responseText, $item)
    {
        $responseCode = (string)$responseCode;
        $code = null;
        $message = null;

        if ($responseCode == '00') {
            return null;
        }

        if (isset($item->Processor) && isset($item->Processor->Response)) {
            $error = self::nvpToArray($item->Processor->Response);
            $code = $error['Code'];
            $message = $error['Message'];
        }

        return new HpsProcessorException($transactionId, $code, $message, $responseCode, $responseText);
    }
85893
final protected function createApiException(ResponseInterface $response, $data, $code = null, $message = null)
    {
        $e = new ApiException(get_class($this).' #'.$code.': '.$message);
        $e->setResponse($response);
        $e->setData($data);
        $e->setErrorCode($code);
        $e->setErrorMessage($message);

        return $e;
    }
641807
public function createExceptionResponse(\Exception $exception)
    {
        $responseData = [
            'code' => $exception->getCode(),
            'type' => static::TYPE_ERROR,
            'message' => $exception->getMessage(),
        ];

        if ($exception instanceof ValidationException) {
            $exception = new \KodiCMS\API\Exceptions\ValidationException($exception->validator);
        }

        if ($exception instanceof Exception or method_exists($exception, 'responseArray')) {
            $responseData = array_merge($responseData, $exception->responseArray());
        } else if ($exception instanceof ModelNotFoundException) {
            $responseData['code'] = static::ERROR_PAGE_NOT_FOUND;
        } else if ($exception instanceof MassAssignmentException) {
            $responseData['code']  = static::ERROR_MISSING_ASSIGMENT;
            $responseData['field'] = $exception->getMessage();
        }

        if ($this->debug) {
            $responseData['file'] = $exception->getFile();
            $responseData['line'] = $exception->getLine();
        }

        return $this->createResponse($responseData, 500);
    }
670841
public function error($errorCode = null, int $statusCode = null, $message = null):JsonResponse
    {
        if ($exception = config("responder.exceptions.$errorCode")) {
            if (class_exists($exception)) {
                throw new $exception();
            }
        }

        return $this->errorResponse->setError($errorCode, $message)->respond($statusCode);
    }
773570
public function display(Exception $exception, $code)
    {
        // Collect some info about the exception.
        $info = $this->info($code, $exception);

        // Is the current request an AJAX request?
        if ((boolean)($this->request instanceof Request && $this->request->wantsJson()) == true) {
            $json_object = (object)[ 'error' => [
                'type'    => 'Exception',
                'message' => $info['message'],
            ] ];

            return JsonResponse::create($json_object, $code);
        }

        // For model-not-found, use 404 errors.
        if ($exception instanceof ModelNotFoundException) {
            $code = 404;
        }

        // If there is a custom view, use that.
        if ($this->view->exists("errors.{$code}")) {
            return $this->view->make("errors.{$code}", $info)->render();
        }

        // If the configured default error view (or errors.error) exists, render that.
        if (($errorview = $this->config->get('view.error', 'errors.error')) && $this->view->exists($errorview)) {
            return $this->view->make($errorview, $info)->render();
        }

        // Last resort: simply show the error code and message.
        return new Response($this->getFallbackDisplay($code, $info['name'], $info['message']), $code);
    }
525238
protected function respond($data, $code, array $headers = [])
    {
        return new JsonResponse($data, $code, array_merge($this->defaultHeaders(), $headers));
    }
810952
protected function getResponse(Request $request, Exception $exception, Exception $transformed)
    {
        $id = $this->container->make(ExceptionIdentifier::class)->identify($exception);

        $flattened = FlattenException::create($transformed);
        $code = $flattened->getStatusCode();
        $headers = $flattened->getHeaders();

        return $this->getDisplayer($request, $exception, $transformed, $code)->display($transformed, $id, $code, $headers);
    }
50843
public function edit($id): Response
    {
        if ($objectInfo = $this->model->findFirst($id)) {
            $request = $this->request->getPut();

            if (empty($request)) {
                $request = $this->request->getJsonRawBody(true);
            }
            //update
            if ($objectInfo->update($request, $this->updateFields)) {
                return $this->response($objectInfo->toArray());
            } else {
                //didnt work
                throw new Exception($objectInfo->getMessages()[0]);
            }
        } else {
            throw new Exception('Record not found');
        }
    }
444661
private function httpExceptionResponse(ServerRequestInterface $request, HttpException $exception): ResponseInterface
    {
        if ($request->getHeaderLine('X-Requested-With') !== '') {
            $this->layout = 'layouts/ajax';
        }

        return $this->viewResponse('components/alert-danger', [
            'alert' => $exception->getMessage(),
            'title' => $exception->getMessage(),
        ], $exception->getStatusCode());
    }