You are here

class Error in JSON-RPC 8

Same name and namespace in other branches
  1. 2.x src/Object/Error.php \Drupal\jsonrpc\Object\Error

Error class to help implement JSON RPC's spec for errors.

Hierarchy

Expanded class hierarchy of Error

6 files declare their use of Error
AddPermissionToRole.php in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/AddPermissionToRole.php
EntityParameterFactory.php in src/ParameterFactory/EntityParameterFactory.php
Handler.php in src/Handler.php
JsonRpcException.php in src/Exception/JsonRpcException.php
Plugins.php in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/Plugins.php

... See full list

File

src/Object/Error.php, line 12

Namespace

Drupal\jsonrpc\Object
View source
class Error implements CacheableDependencyInterface {
  use CacheableDependencyTrait;
  const PARSE_ERROR = -32700;
  const INVALID_REQUEST = -32600;
  const METHOD_NOT_FOUND = -32601;
  const INVALID_PARAMS = -32602;
  const INTERNAL_ERROR = -32603;
  public static $errorMessages = [
    -32700 => 'Parse Error',
    -32600 => 'Invalid Request',
    -32601 => 'Method Not Found',
    -32602 => 'Invalid Params',
    -32603 => 'Internal Error',
  ];
  public static $errorMeanings = [
    -32700 => 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
    -32600 => 'The JSON sent is not a valid Request object.',
    -32601 => "The method '%s' does not exist/is not available.",
    -32602 => 'Invalid method parameter(s).',
    -32603 => 'Internal JSON-RPC error.',
  ];

  /**
   * The error's type code.
   *
   * @var int
   */
  protected $code;

  /**
   * The error's short description.
   *
   * @var string
   */
  protected $message;

  /**
   * Additional information about the error.
   *
   * @var mixed
   */
  protected $data;

  /**
   * Error constructor.
   *
   * @param int $code
   *   The error's type code.
   * @param string $message
   *   The error's short description.
   * @param mixed $data
   *   (optional) A primitive or structured value that contains additional
   *   information about the error. This may be omitted.
   * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
   *   (optional) A cacheable dependency.
   */
  public function __construct($code, $message, $data = NULL, CacheableDependencyInterface $cacheability = NULL) {
    $this
      ->assertValidError($code, $message);
    $this->code = $code;
    $this->message = $message;
    if (!is_null($data)) {
      $this->data = $data;
    }
    $this
      ->setCacheability($cacheability ?: new CacheableMetadata());
  }

  /**
   * Get the error's type code.
   *
   * @return int
   *   The error code.
   */
  public function getCode() {
    return $this->code;
  }

  /**
   * Get the error's short description.
   *
   * @return string
   *   The error message.
   */
  public function getMessage() {
    return $this->message;
  }

  /**
   * Get additional information about the error.
   *
   * @return mixed
   *   The additional data about the error.
   */
  public function getData() {
    return $this->data;
  }

  /**
   * Asserts that the error is valid.
   *
   * @param mixed $code
   *   The HTTP code.
   * @param mixed $message
   *   The output message.
   */
  protected function assertValidError($code, $message) {
    assert(is_int($code) && !($code >= -32000 && $code <= -32099), "The {$code} code is reserved for implementation-defined server-errors.");
    assert(is_string($message) && strlen($message) < 256, 'The message SHOULD be limited to a concise single sentence.');
  }

  /**
   * Constructs a new parse error.
   *
   * @param mixed $data
   *   More specific information about the error.
   *
   * @return static
   */
  public static function parseError($data = NULL) {
    return new static(static::PARSE_ERROR, static::$errorMessages[static::PARSE_ERROR], $data ?: static::$errorMeanings[static::PARSE_ERROR]);
  }

  /**
   * Constructs a new invalid request error.
   *
   * @param mixed $data
   *   More specific information about the error.
   * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
   *   (optional) A cacheable dependency.
   *
   * @return static
   */
  public static function invalidRequest($data = NULL, CacheableDependencyInterface $cacheability = NULL) {
    return new static(static::INVALID_REQUEST, static::$errorMessages[static::INVALID_REQUEST], $data ?: static::$errorMeanings[static::INVALID_REQUEST], $cacheability);
  }

  /**
   * Constructs a new method not found error.
   *
   * @param string $method_name
   *   The name of the missing method.
   * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
   *   (optional) A cacheable dependency.
   *
   * @return static
   */
  public static function methodNotFound($method_name, CacheableDependencyInterface $cacheability = NULL) {
    $data = sprintf(static::$errorMeanings[static::METHOD_NOT_FOUND], $method_name);
    return new static(static::METHOD_NOT_FOUND, static::$errorMessages[static::METHOD_NOT_FOUND], $data, $cacheability);
  }

  /**
   * Constructs a new invalid params error.
   *
   * @param mixed $data
   *   More specific information about the error.
   * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
   *   (optional) A cacheable dependency.
   *
   * @return static
   */
  public static function invalidParams($data = NULL, CacheableDependencyInterface $cacheability = NULL) {
    return new static(static::INVALID_PARAMS, static::$errorMessages[static::INVALID_PARAMS], $data ?: static::$errorMeanings[static::INVALID_PARAMS], $cacheability);
  }

  /**
   * Constructs a new internal error.
   *
   * @param mixed $data
   *   More specific information about the error.
   * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
   *   (optional) A cacheable dependency.
   *
   * @return static
   */
  public static function internalError($data = NULL, CacheableDependencyInterface $cacheability = NULL) {
    return new static(static::INTERNAL_ERROR, static::$errorMessages[static::INTERNAL_ERROR], $data ?: static::$errorMeanings[static::INTERNAL_ERROR], $cacheability);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 3
CacheableDependencyTrait::getCacheMaxAge public function 3
CacheableDependencyTrait::getCacheTags public function 3
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
Error::$code protected property The error's type code.
Error::$data protected property Additional information about the error.
Error::$errorMeanings public static property
Error::$errorMessages public static property
Error::$message protected property The error's short description.
Error::assertValidError protected function Asserts that the error is valid.
Error::getCode public function Get the error's type code.
Error::getData public function Get additional information about the error.
Error::getMessage public function Get the error's short description.
Error::internalError public static function Constructs a new internal error.
Error::INTERNAL_ERROR constant
Error::invalidParams public static function Constructs a new invalid params error.
Error::invalidRequest public static function Constructs a new invalid request error.
Error::INVALID_PARAMS constant
Error::INVALID_REQUEST constant
Error::methodNotFound public static function Constructs a new method not found error.
Error::METHOD_NOT_FOUND constant
Error::parseError public static function Constructs a new parse error.
Error::PARSE_ERROR constant
Error::__construct public function Error constructor.