From 8ff0f798a5cf372a9b6440f17c3d09f6a8a84cf1 Mon Sep 17 00:00:00 2001 From: dmazurek Date: Sat, 24 Feb 2018 08:59:58 +0100 Subject: [PATCH 1/2] Allow passing a message to JsonRpcException --- src/exception/InvalidParams.php | 4 ++-- src/exception/InvalidRequest.php | 4 ++-- src/exception/MethodNotFound.php | 4 ++-- src/exception/ParseError.php | 4 ++-- tests/unit/exception/InvalidParamsTest.php | 4 ++-- tests/unit/exception/InvalidRequestTest.php | 4 ++-- tests/unit/exception/MethodNotFoundTest.php | 4 ++-- tests/unit/exception/ParseErrorTest.php | 7 ++++--- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/exception/InvalidParams.php b/src/exception/InvalidParams.php index 7695676..6ec4e7a 100644 --- a/src/exception/InvalidParams.php +++ b/src/exception/InvalidParams.php @@ -8,8 +8,8 @@ class InvalidParams extends JsonRpcException { - public function __construct() + public function __construct(string $message = '') { - parent::__construct("Invalid params", JsonRpcErrorCodes::INVALID_PARAMS); + parent::__construct("Invalid params: " . $message , JsonRpcErrorCodes::INVALID_PARAMS); } } diff --git a/src/exception/InvalidRequest.php b/src/exception/InvalidRequest.php index 1e53566..5327d6d 100644 --- a/src/exception/InvalidRequest.php +++ b/src/exception/InvalidRequest.php @@ -8,8 +8,8 @@ class InvalidRequest extends JsonRpcException { - public function __construct() + public function __construct(string $message = '') { - parent::__construct("Invalid request", JsonRpcErrorCodes::INVALID_REQUEST); + parent::__construct("Invalid request: " . $message, JsonRpcErrorCodes::INVALID_REQUEST); } } diff --git a/src/exception/MethodNotFound.php b/src/exception/MethodNotFound.php index 5e67b08..05e915d 100644 --- a/src/exception/MethodNotFound.php +++ b/src/exception/MethodNotFound.php @@ -8,8 +8,8 @@ class MethodNotFound extends JsonRpcException { - public function __construct() + public function __construct(string $message = '') { - parent::__construct("Method not found", JsonRpcErrorCodes::METHOD_NOT_FOUND); + parent::__construct("Method not found: " . $message, JsonRpcErrorCodes::METHOD_NOT_FOUND); } } diff --git a/src/exception/ParseError.php b/src/exception/ParseError.php index 8ede5fa..a6c56fc 100644 --- a/src/exception/ParseError.php +++ b/src/exception/ParseError.php @@ -8,8 +8,8 @@ class ParseError extends JsonRpcException { - public function __construct() + public function __construct(string $message = '') { - parent::__construct("Parse error", JsonRpcErrorCodes::PARSE_ERROR); + parent::__construct("Parse error:" .$message , JsonRpcErrorCodes::PARSE_ERROR); } } diff --git a/tests/unit/exception/InvalidParamsTest.php b/tests/unit/exception/InvalidParamsTest.php index ce2da81..2ae5e4b 100644 --- a/tests/unit/exception/InvalidParamsTest.php +++ b/tests/unit/exception/InvalidParamsTest.php @@ -14,8 +14,8 @@ class InvalidParamsTest extends TestCase */ public function givesCorrectErrorInfo() { - $exception = new InvalidParams(); + $exception = new InvalidParams('Missing param id'); $this->assertEquals(JsonRpcErrorCodes::INVALID_PARAMS, $exception->getCode()); - $this->assertEquals('Invalid params', $exception->getMessage()); + $this->assertEquals('Invalid params: Missing param id', $exception->getMessage()); } } diff --git a/tests/unit/exception/InvalidRequestTest.php b/tests/unit/exception/InvalidRequestTest.php index 331aa76..be54cce 100644 --- a/tests/unit/exception/InvalidRequestTest.php +++ b/tests/unit/exception/InvalidRequestTest.php @@ -14,8 +14,8 @@ class InvalidRequestTest extends TestCase */ public function givesCorrectErrorInfo() { - $exception = new InvalidRequest(); + $exception = new InvalidRequest('Request'); $this->assertEquals(JsonRpcErrorCodes::INVALID_REQUEST, $exception->getCode()); - $this->assertEquals('Invalid request', $exception->getMessage()); + $this->assertEquals('Invalid request: Request', $exception->getMessage()); } } diff --git a/tests/unit/exception/MethodNotFoundTest.php b/tests/unit/exception/MethodNotFoundTest.php index 8796ce3..ad3f41b 100644 --- a/tests/unit/exception/MethodNotFoundTest.php +++ b/tests/unit/exception/MethodNotFoundTest.php @@ -14,8 +14,8 @@ class MethodNotFoundTest extends TestCase */ public function givesCorrectErrorInfo() { - $exception = new MethodNotFound(); + $exception = new MethodNotFound('sampleMethod'); $this->assertEquals(JsonRpcErrorCodes::METHOD_NOT_FOUND, $exception->getCode()); - $this->assertEquals('Method not found', $exception->getMessage()); + $this->assertEquals('Method not found: sampleMethod', $exception->getMessage()); } } diff --git a/tests/unit/exception/ParseErrorTest.php b/tests/unit/exception/ParseErrorTest.php index 675c40a..2403c18 100644 --- a/tests/unit/exception/ParseErrorTest.php +++ b/tests/unit/exception/ParseErrorTest.php @@ -5,16 +5,17 @@ namespace DawidMazurek\JsonRpc\exception; use DawidMazurek\JsonRpc\error\JsonRpcErrorCodes; +use PHPUnit\Framework\TestCase; -class ParseErrorTest +class ParseErrorTest extends TestCase { /** * @test */ public function givesCorrectErrorInfo() { - $exception = new ParseError(); + $exception = new ParseError('Received empty body'); $this->assertEquals(JsonRpcErrorCodes::PARSE_ERROR, $exception->getCode()); - $this->assertEquals('parseError', $exception->getMessage()); + $this->assertEquals('parseError: Received empty body', $exception->getMessage()); } } From 692b63479fba64413e61f7216f486426e124e459 Mon Sep 17 00:00:00 2001 From: dmazurek Date: Sat, 24 Feb 2018 09:04:27 +0100 Subject: [PATCH 2/2] parse error test --- src/exception/ParseError.php | 2 +- tests/unit/exception/ParseErrorTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/exception/ParseError.php b/src/exception/ParseError.php index a6c56fc..98478eb 100644 --- a/src/exception/ParseError.php +++ b/src/exception/ParseError.php @@ -10,6 +10,6 @@ class ParseError extends JsonRpcException { public function __construct(string $message = '') { - parent::__construct("Parse error:" .$message , JsonRpcErrorCodes::PARSE_ERROR); + parent::__construct("Parse error: " .$message , JsonRpcErrorCodes::PARSE_ERROR); } } diff --git a/tests/unit/exception/ParseErrorTest.php b/tests/unit/exception/ParseErrorTest.php index 2403c18..9fbbffb 100644 --- a/tests/unit/exception/ParseErrorTest.php +++ b/tests/unit/exception/ParseErrorTest.php @@ -16,6 +16,6 @@ public function givesCorrectErrorInfo() { $exception = new ParseError('Received empty body'); $this->assertEquals(JsonRpcErrorCodes::PARSE_ERROR, $exception->getCode()); - $this->assertEquals('parseError: Received empty body', $exception->getMessage()); + $this->assertEquals('Parse error: Received empty body', $exception->getMessage()); } }