protected function buildErrorResponse($error, $code = null)
    {
        if ($code !== null && (!is_int($code) || $code < 400)) {
            $code = null;
        }
        if (is_object($error)) {
            if ($error instanceof AccessDeniedException) {
                $error = $error->getMessage();
                if ($code === null) {
                    $code = Response::HTTP_UNAUTHORIZED;
                }
            } elseif ($error instanceof UserMessageException) {
                if ($error->getCode() >= 400) {
                    $code = $error->getCode();
                }
                $error = $error->getMessage();
            } elseif ($error instanceof Exception || $error instanceof Throwable) {
                $error = t('An unexpected error occurred');
            } elseif (is_callable([$error, '__toString'])) {
                $error = (string) $error;
            } else {
                $error = get_class($error);
            }
        }
        if ($code === null) {
            $code = Response::HTTP_INTERNAL_SERVER_ERROR;
        }

        return $this->getResponseFactory()->create(
            $error,
            $code,
            [
                'Content-Type' => 'text/plain; charset=UTF-8',
            ]
        );
    }
protected function buildErrorResponse($error, $code = null)
    {
        if ($code !== null && (!is_int($code) || $code < 400)) {
            $code = null;
        }
        if (is_object($error)) {
            if ($error instanceof AccessDeniedException) {
                $error = $error->getMessage();
                if ($code === null) {
                    $code = Response::HTTP_UNAUTHORIZED;
                }
            } elseif ($error instanceof UserMessageException) {
                if ($error->getCode() >= 400) {
                    $code = $error->getCode();
                }
                $error = $error->getMessage();
            } elseif ($error instanceof Exception || $error instanceof Throwable) {
                $error = t('An unexpected error occurred');
            } elseif (is_callable([$error, '__toString'])) {
                $error = (string) $error;
            } else {
                $error = get_class($error);
            }
        }
        if ($code === null) {
            $code = Response::HTTP_INTERNAL_SERVER_ERROR;
        }

        return $this->getResponseFactory()->create(
            $error,
            $code,
            [
                'Content-Type' => 'text/plain; charset=UTF-8',
            ]
        );
    }
protected function buildErrorResponse ( $ error , $ code = null ) { if ( $ code !== null && ( ! is_int ( $ code ) || $ code < 400 ) ) { $ code = null ; } if ( is_object ( $ error ) ) { if ( $ error instanceof AccessDeniedException ) { $ error = $ error -> getMessage ( ) ; if ( $ code === null ) { $ code = Response :: HTTP_UNAUTHORIZED ; } } elseif ( $ error instanceof UserMessageException ) { if ( $ error -> getCode ( ) >= 400 ) { $ code = $ error -> getCode ( ) ; } $ error = $ error -> getMessage ( ) ; } elseif ( $ error instanceof Exception || $ error instanceof Throwable ) { $ error = t ( 'An unexpected error occurred' ) ; } elseif ( is_callable ( [ $ error , '__toString' ] ) ) { $ error = ( string ) $ error ; } else { $ error = get_class ( $ error ) ; } } if ( $ code === null ) { $ code = Response :: HTTP_INTERNAL_SERVER_ERROR ; } return $ this -> getResponseFactory ( ) -> create ( $ error , $ code , [ 'Content-Type' => 'text/plain; charset=UTF-8' , ] ) ; }
Build an error response.

@param string|Exception|Throwable $error
@param int $code

@return Response
Build an error response .