Skip to content

Commit 8ff0f79

Browse files
committed
Allow passing a message to JsonRpcException
1 parent a0c3391 commit 8ff0f79

File tree

8 files changed

+18
-17
lines changed

8 files changed

+18
-17
lines changed

src/exception/InvalidParams.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class InvalidParams extends JsonRpcException
1010
{
11-
public function __construct()
11+
public function __construct(string $message = '')
1212
{
13-
parent::__construct("Invalid params", JsonRpcErrorCodes::INVALID_PARAMS);
13+
parent::__construct("Invalid params: " . $message , JsonRpcErrorCodes::INVALID_PARAMS);
1414
}
1515
}

src/exception/InvalidRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class InvalidRequest extends JsonRpcException
1010
{
11-
public function __construct()
11+
public function __construct(string $message = '')
1212
{
13-
parent::__construct("Invalid request", JsonRpcErrorCodes::INVALID_REQUEST);
13+
parent::__construct("Invalid request: " . $message, JsonRpcErrorCodes::INVALID_REQUEST);
1414
}
1515
}

src/exception/MethodNotFound.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class MethodNotFound extends JsonRpcException
1010
{
11-
public function __construct()
11+
public function __construct(string $message = '')
1212
{
13-
parent::__construct("Method not found", JsonRpcErrorCodes::METHOD_NOT_FOUND);
13+
parent::__construct("Method not found: " . $message, JsonRpcErrorCodes::METHOD_NOT_FOUND);
1414
}
1515
}

src/exception/ParseError.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class ParseError extends JsonRpcException
1010
{
11-
public function __construct()
11+
public function __construct(string $message = '')
1212
{
13-
parent::__construct("Parse error", JsonRpcErrorCodes::PARSE_ERROR);
13+
parent::__construct("Parse error:" .$message , JsonRpcErrorCodes::PARSE_ERROR);
1414
}
1515
}

tests/unit/exception/InvalidParamsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class InvalidParamsTest extends TestCase
1414
*/
1515
public function givesCorrectErrorInfo()
1616
{
17-
$exception = new InvalidParams();
17+
$exception = new InvalidParams('Missing param id');
1818
$this->assertEquals(JsonRpcErrorCodes::INVALID_PARAMS, $exception->getCode());
19-
$this->assertEquals('Invalid params', $exception->getMessage());
19+
$this->assertEquals('Invalid params: Missing param id', $exception->getMessage());
2020
}
2121
}

tests/unit/exception/InvalidRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class InvalidRequestTest extends TestCase
1414
*/
1515
public function givesCorrectErrorInfo()
1616
{
17-
$exception = new InvalidRequest();
17+
$exception = new InvalidRequest('Request');
1818
$this->assertEquals(JsonRpcErrorCodes::INVALID_REQUEST, $exception->getCode());
19-
$this->assertEquals('Invalid request', $exception->getMessage());
19+
$this->assertEquals('Invalid request: Request', $exception->getMessage());
2020
}
2121
}

tests/unit/exception/MethodNotFoundTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class MethodNotFoundTest extends TestCase
1414
*/
1515
public function givesCorrectErrorInfo()
1616
{
17-
$exception = new MethodNotFound();
17+
$exception = new MethodNotFound('sampleMethod');
1818
$this->assertEquals(JsonRpcErrorCodes::METHOD_NOT_FOUND, $exception->getCode());
19-
$this->assertEquals('Method not found', $exception->getMessage());
19+
$this->assertEquals('Method not found: sampleMethod', $exception->getMessage());
2020
}
2121
}

tests/unit/exception/ParseErrorTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
namespace DawidMazurek\JsonRpc\exception;
66

77
use DawidMazurek\JsonRpc\error\JsonRpcErrorCodes;
8+
use PHPUnit\Framework\TestCase;
89

9-
class ParseErrorTest
10+
class ParseErrorTest extends TestCase
1011
{
1112
/**
1213
* @test
1314
*/
1415
public function givesCorrectErrorInfo()
1516
{
16-
$exception = new ParseError();
17+
$exception = new ParseError('Received empty body');
1718
$this->assertEquals(JsonRpcErrorCodes::PARSE_ERROR, $exception->getCode());
18-
$this->assertEquals('parseError', $exception->getMessage());
19+
$this->assertEquals('parseError: Received empty body', $exception->getMessage());
1920
}
2021
}

0 commit comments

Comments
 (0)