class JsonRpcException in JSON-RPC 8
Same name and namespace in other branches
- 2.x src/Exception/JsonRpcException.php \Drupal\jsonrpc\Exception\JsonRpcException
Custom exception class for the module.
Hierarchy
- class \Drupal\jsonrpc\Exception\JsonRpcException extends \Drupal\jsonrpc\Exception\Exception implements CacheableDependencyInterface uses CacheableDependencyTrait
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
File
- src/
Exception/ JsonRpcException.php, line 13
Namespace
Drupal\jsonrpc\ExceptionView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
JsonRpcException:: |
protected | property | The JSON-RPC error response for the exception. | |
JsonRpcException:: |
protected static | function | Helper to build a JSON-RPC response object. | |
JsonRpcException:: |
public static | function | Constructs a JsonRpcException from an arbitrary error object. | |
JsonRpcException:: |
public static | function | Constructs a JsonRpcException from an arbitrary exception. | |
JsonRpcException:: |
public | function | The appropriate JSON-RPC error response for the exception. | |
JsonRpcException:: |
public | function | JsonRpcException constructor. |