Produces a successful response
@param string $message
@param array $errors
@param int $code default 404
176102
public function prepareError($message, $errors=[], $code=404) {
        $this->prepareResult(false, $message, $code, null , $errors);
    }
802749
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',
            ]
        );
    }
319501
protected function sendError(
        int $code,
        string $message,
        $data = null
    ) {
        $response = $this->prepareResponse();

        switch ($code) {
            case static::ERROR_INTERNAL_SERVER:
                $status = HttpResponse::HTTP_INTERNAL_SERVER_ERROR;
                break;

            case static::ERROR_INVALID_CREDENTIALS:
            case static::ERROR_UNAUTHENTICATED:
                $status = HttpResponse::HTTP_UNAUTHORIZED;
                $realm = $this->authentication['realm'] ?? null;
                $auth = 'Basic'
                    . ($realm !== null ? ' realm="' . $realm . '"' : '')
                    . ' charset="UTF-8"';
                $response->headers['WWW-Authenticate'] = $auth;
                break;

            case static::ERROR_INVALID_TOKEN:
                $status = HttpResponse::HTTP_UNAUTHORIZED;
                $realm = $this->authentication['realm'] ?? null;
                $auth = 'Bearer'
                    . ($realm !== null ? ' realm="' . $realm . '"' : '');
                $response->headers['WWW-Authenticate'] = $auth;
                break;

            case static::ERROR_METHOD_NOT_IMPLEMENTED:
                $status = HttpResponse::HTTP_NOT_IMPLEMENTED;
                $response->headers['Allow'] = $data;
                break;

            case static::ERROR_UNAUTHORIZED:
                $status = HttpResponse::HTTP_FORBIDDEN;
                break;

            case static::ERROR_INVALID_RESOURCE:
            case static::ERROR_INVALID_RESOURCE_ID:
            case static::ERROR_INVALID_RESOURCE_OFFSET:
            case static::ERROR_INVALID_RESOURCE_FOREIGN:
            case static::ERROR_INVALID_PAYLOAD:
            case static::ERROR_INVALID_QUERY_PARAMETER:
            case static::ERROR_UNKNOWN_FIELDS:
            case static::ERROR_READONLY_RESOURCE:
            case static::ERROR_FOREIGN_CONSTRAINT:
                $status = HttpResponse::HTTP_BAD_REQUEST;
                break;

            case static::ERROR_RESOURCE_NOT_FOUND:
                $status = HttpResponse::HTTP_NOT_FOUND;
                break;

            case static::ERROR_UNSUPPORTED_MEDIA_TYPE:
                $status = HttpResponse::HTTP_UNSUPPORTED_MEDIA_TYPE;
                break;

            case static::ERROR_METHOD_NOT_ALLOWED:
                $status = HttpResponse::HTTP_METHOD_NOT_ALLOWED;
                if ($data !== null) {
                    $response->headers['Allow'] = $data;
                }
                break;

            case static::ERROR_NOT_ACCEPTABLE:
                $status = HttpResponse::HTTP_NOT_ACCEPTABLE;
                break;

            case static::ERROR_UNKNOWN_ERROR:
            default:
                $actual_code = static::ERROR_UNKNOWN_ERROR;
                $status = HttpResponse::HTTP_INTERNAL_SERVER_ERROR;
                $message = 'Unknown error'
                    . (strlen($message) > 0 ? ': ' . $message : '');
                break;
        }
        $response->status = $status;

        $response->headers['Content-Type'] = 'application/json';
        $response->body = [
            'code' => $code,
            'message' => $message,
        ];

        throw new RouterException($response, $actual_code ?? $code);
    }
1021436
public function notFound($msg = '', $errorCode = null, $headers = [])
    {
        return $this->getErrorResponse($msg, $errorCode, self::HTTP_NOT_FOUND, $headers);
    }
722962
protected final function error($code, $message = null) {
        if (empty($message) && is_string($code)) {
            $msg = explode('@', $code);
            if (count($msg) >= 2) {
                $message = $msg[1];
                $code    = intval($msg[0]);
            } else {
                $message = $code;
                $code    = 500;
            }
        } else if (empty($message)) {
            $message = __('Internal Error');
        }
        throw new RestException($message, $code);
    }
1139716
public function error(array $errors): Response
    {
        $this->response->setStatus(500);
        $bodyTemplate = '<html><title>Error 500</title><h1>Server Error</h1><pre>%s</pre></html>';
        $this->response->setBody(sprintf($bodyTemplate, print_r($errors, true)));
        return $this->response;
    }
698636
public function response(array $errors)
    {
        return $this->ajaxRequest
            ? $this->formatJsonErrorsResponse($errors)
            : parent::response($errors);
    }
1034307
protected function respondWithMessage($success = true, $message = '', $code = 0)
    {
        if (!$message) {
            if ($success) {
                $message = rawurlencode(_t(
                    'CMSMain.ACTION_COMPLETE',
                    'Done'
                ));
            } else {
                $message = rawurlencode(_t(
                    'CMSMain.ACTION_FAILED',
                    'Could not complete action'
                ));
            }
        }

        if (!$code) {
            $code = $success ? 200 : 400;
        }

        $this->owner->Response->setStatusCode($code);
        $this->owner->Response->addHeader('X-Status', $message);

        return $this->owner->getResponseNegotiator()->respond($this->owner->Request, [
            'SiteTree' => function () {
            }
        ]);
    }
627248
public static function response(string $message = null, int $code = null, array $headers = [])
  {
    $rest = new Rest;
    $rest->message = $message;

    if ($headers != []) {
      foreach($headers as $field => $i) {
        array_push($rest->headers, $field .': '. $i);
      }
    }

    if ($code != null) {
      $rest->code = $code;
      header('HTTP/1.1 '.$rest->code);
      return $rest->send();
    }

    return $rest;
  }
291784
protected static function loadResult($status, $code, $message, $errors, $extras)
    {
        $Result = new static($status);

        if (! empty($code)) {
            $Result->setCode($code);
        }

        if (! empty($message)) {
            $Result->setMessage($message);
        }

        if (! empty($errors)) {
            $Result->setErrors($errors);
        }

        if (! empty($extras)) {
            $Result->setExtras($extras);
        }

        return $Result;
    }
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);
    }
802759
public function respondExceptionError($status, $message, $status_code, $payload)
    {
        $responseData = $this->getResponseStructure($status, $payload, $message);
        $response = new JsonResponse($responseData, $status_code, $this->getHeaders());

        return $response;
    }
65741
public function setErrorResponse($message, $description = '', $code = '')
    {
        $this->outputArray['errorReport']['message'] = $message;

        if (!empty($description)) {
            $this->outputArray['errorReport']['description'] = $description;
        }

        if (!empty($code)) {
            $this->outputArray['errorReport']['code'] = $code;
        }

        return $this;
    }
1289552
public function success(int $statusCode, string $message, $data = null): JsonResponse
    {

        $this->setStatusCode($statusCode);

        $this->setMessage($message);

        $this->setData($data);


        $content['message'] = $this->getMessage();


        if (null !== $this->getCode()) {
            $content['code'] = $this->getCode();
        }


        $content['status_code'] = $this->getStatusCode();


        if (null !== $this->getData() && $this->getErrors() !== $this->getData()) {
            $content['data'] = $this->getData();
        }

        if (null !== $this->getErrors()) {
            $content['errors'] = $this->getErrors();
        }

        $content += $this->Hypermedia;

        return new JsonResponse($content, $this->getHttpStatusCode() ?: $this->getStatusCode());
    }
274022
public function response(array $errors)
    {
        if (($this->ajax() && ! $this->pjax()) || $this->wantsJson()) {
            return new JsonResponse($errors, ValidationException::VALIDATION_FAILED);
        }

        return $this->redirector->to($this->getRedirectUrl())
            ->withInput($this->except($this->dontFlash))
            // This makes errors display properly (original errors are placed under the key 'errors' instead of 'error')
            ->with('error', $this->getValidatorInstance()->getMessageBag());
    }
956509
public function error( $code = 404, $msg = "Resource not found" )
	{
		$request = HttpFactory::i()->request();

		if ( $request->ajax() || $request->wantsJson() )
			return new JsonResponse( ['error' => $msg] );
		else
			return new Response( Redirect::back()->withInput( $request->input() )->withErrors( ['error' => $msg] ) );
	}
927542
public static function codeResponse($code,$message)
    {
        $code = intval($code);

        foreach(self::$results as $v) {
            if ($v['message'] == $message && $v['code'] == $code)
                return $v;
        }

        return null;
    }