Skip to content

Commit

Permalink
Add checks for properties on error object
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed May 17, 2024
1 parent dab7433 commit 065231a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,34 @@ public static function createResponseMessageV1($id,$result=null,$error=null): Re
/**
* @param $id
* @param $result
* @param object|string|null $error
* @param object|null $error
* @return RequestMessage
* @throws JSONRPCException
*/
public static function createResponseMessageV2($id,$result=null,$error=null): RequestMessage{
public static function createResponseMessageV2($id,$result=null,?object $error=null): RequestMessage{
if(!is_null($result) && !is_null($error)){
throw new JSONRPCException('[V2] Only one property "result" or "error" can be non null.');
}
if(!is_object($error) && !is_null($error)){
throw new JSONRPCException('[V1] The "error" property in request MUST be an object or null.');
throw new JSONRPCException('[V2] The "error" property in request MUST be an object or null.');
}
$arr = [
'jsonrpc' => '2.0',
'id' => $id,
];
if(!is_null($error)){
if(!property_exists($error,'code')){
throw new JSONRPCException('[V2] The error object MUST have a "code" property.');
}
if(!property_exists($error,'message')){
throw new JSONRPCException('[V2] The error object MUST have a "message" property.');
}
if(!is_int($error->code)){
throw new JSONRPCException('[V2] The "code" property of the error object MUST be an integer.');
}
if(!is_string($error->message)){
throw new JSONRPCException('[V2] The "message" property of the error object MUST be a string.');
}
$arr['error'] = $error;
}else{
$arr['result'] = $result;
Expand Down
23 changes: 23 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ public function testIsVersion2(){
$this->assertTrue(Message::createResponseMessageV2(123,'myMethod')->isVersion2());
}

/**
* @return void
* @throws JSONRPCException
*/
public function testCreateRequestMessageV2WithIdAndMethodAndParamsFalse(){
$this->expectException(JSONRPCException::class);
$this->expectExceptionMessage('[V2] The "params" property in request MUST be an object, array or null.');

Message::createRequestMessageV2(123,'abc',false);
}

/**
* @return void
* @throws JSONRPCException
Expand All @@ -431,6 +442,18 @@ public function testCreateResponseMessageV1WithErrorFalse(){
Message::createResponseMessageV1(123,null,false);
}

/**
* @return void
* @throws JSONRPCException
*/
public function testCreateRequestOrNotificationV2(){
$this->assertInstanceOf(RequestMessage::class,Message::createRequestMessageV2(123,'myMethod',[]));
$this->assertInstanceOf(RequestMessage::class,Message::createRequestMessageV2(123,'myMethod',(object) []));

$this->assertInstanceOf(NotificationMessage::class,Message::createNotificationMessageV2('myMethod',[]));
$this->assertInstanceOf(NotificationMessage::class,Message::createNotificationMessageV2('myMethod',(object) []));
}

public function testToObject(){
$this->assertEquals((object) ['id'=>123,'method'=>'getMethod','params'=>['param1','param2']],Message::createRequestMessageV1(123,'getMethod',['param1','param2'])->toObject());
}
Expand Down

0 comments on commit 065231a

Please sign in to comment.