View source
<?php
namespace Drupal\jsonrpc\Object;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableDependencyTrait;
use Drupal\Core\Cache\CacheableMetadata;
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.',
];
protected $code;
protected $message;
protected $data;
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());
}
public function getCode() {
return $this->code;
}
public function getMessage() {
return $this->message;
}
public function getData() {
return $this->data;
}
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.');
}
public static function parseError($data = NULL) {
return new static(static::PARSE_ERROR, static::$errorMessages[static::PARSE_ERROR], $data ?: static::$errorMeanings[static::PARSE_ERROR]);
}
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);
}
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);
}
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);
}
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);
}
}