You are here

class JsonRpcException in JSON-RPC 8

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

Custom exception class for the module.

Hierarchy

Expanded class hierarchy of JsonRpcException

7 files declare their use of JsonRpcException
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
HttpController.php in src/Controller/HttpController.php
Plugins.php in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/Plugins.php

... See full list

File

src/Exception/JsonRpcException.php, line 13

Namespace

Drupal\jsonrpc\Exception
View source
class JsonRpcException extends \Exception implements CacheableDependencyInterface {
  use CacheableDependencyTrait;

  /**
   * The JSON-RPC error response for the exception.
   *
   * @var \Drupal\jsonrpc\Object\Response
   *   The RPC response object.
   */
  protected $response;

  /**
   * JsonRpcException constructor.
   *
   * @param \Drupal\jsonrpc\Object\Response $response
   *   The JSON-RPC error response object for the exception.
   * @param \Throwable $previous
   *   The previous exception.
   */
  public function __construct(Response $response, \Throwable $previous = NULL) {
    $this->response = $response;
    $error = $response
      ->getError();
    $this
      ->setCacheability($response);
    parent::__construct($error
      ->getMessage(), $error
      ->getCode(), $previous);
  }

  /**
   * The appropriate JSON-RPC error response for the exception.
   *
   * @return \Drupal\jsonrpc\Object\Response
   *   The RPC response object.
   */
  public function getResponse() {
    return $this->response;
  }

  /**
   * Constructs a JsonRpcException from an arbitrary exception.
   *
   * @param \Throwable|\Exception $previous
   *   An arbitrary exception.
   * @param mixed $id
   *   The request ID, if available.
   * @param string $version
   *   (optional) The JSON-RPC version.
   *
   * @return static
   */
  public static function fromPrevious($previous, $id = FALSE, $version = NULL) {
    if ($previous instanceof JsonRpcException) {

      // Ensures that the ID and version context information are set because it
      // might not have been set or accessible at a lower level.
      $response = $previous
        ->getResponse();
      return static::fromError($response
        ->getError(), $response
        ->id() ?: $id, $response
        ->version());
    }
    $error = Error::internalError($previous
      ->getMessage());
    $response = static::buildResponse($error, $id, $version);
    return new static($response, $previous);
  }

  /**
   * Constructs a JsonRpcException from an arbitrary error object.
   *
   * @param \Drupal\jsonrpc\Object\Error $error
   *   The error which caused the exception.
   * @param mixed $id
   *   The request ID, if available.
   * @param string $version
   *   (optional) The JSON-RPC version.
   *
   * @return static
   */
  public static function fromError(Error $error, $id = FALSE, $version = NULL) {
    return new static(static::buildResponse($error, $id, $version));
  }

  /**
   * Helper to build a JSON-RPC response object.
   *
   * @param \Drupal\jsonrpc\Object\Error $error
   *   The error object.
   * @param mixed $id
   *   The request ID.
   * @param string $version
   *   The information version.
   *
   * @return \Drupal\jsonrpc\Object\Response
   *   The RPC response object.
   */
  protected static function buildResponse(Error $error, $id = FALSE, $version = NULL) {
    $supported_version = $version ?: \Drupal::service('jsonrpc.handler')
      ->supportedVersion();
    return new Response($supported_version, $id ? $id : NULL, NULL, $error);
  }

}

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.
JsonRpcException::$response protected property The JSON-RPC error response for the exception.
JsonRpcException::buildResponse protected static function Helper to build a JSON-RPC response object.
JsonRpcException::fromError public static function Constructs a JsonRpcException from an arbitrary error object.
JsonRpcException::fromPrevious public static function Constructs a JsonRpcException from an arbitrary exception.
JsonRpcException::getResponse public function The appropriate JSON-RPC error response for the exception.
JsonRpcException::__construct public function JsonRpcException constructor.