Skip to content

Commit

Permalink
Merge pull request #12 from DawidMazurek/allow-message-in-exceptions
Browse files Browse the repository at this point in the history
Allow passing a message to JsonRpcException
  • Loading branch information
DawidMazurek authored Feb 24, 2018
2 parents a0c3391 + 692b634 commit 90313d3
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/exception/InvalidParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/exception/InvalidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/exception/MethodNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/exception/ParseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions tests/unit/exception/InvalidParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
4 changes: 2 additions & 2 deletions tests/unit/exception/InvalidRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
4 changes: 2 additions & 2 deletions tests/unit/exception/MethodNotFoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
7 changes: 4 additions & 3 deletions tests/unit/exception/ParseErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('Parse error: Received empty body', $exception->getMessage());
}
}

0 comments on commit 90313d3

Please sign in to comment.