Skip to content

Commit 065231a

Browse files
committed
Add checks for properties on error object
1 parent dab7433 commit 065231a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/Message.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,34 @@ public static function createResponseMessageV1($id,$result=null,$error=null): Re
248248
/**
249249
* @param $id
250250
* @param $result
251-
* @param object|string|null $error
251+
* @param object|null $error
252252
* @return RequestMessage
253253
* @throws JSONRPCException
254254
*/
255-
public static function createResponseMessageV2($id,$result=null,$error=null): RequestMessage{
255+
public static function createResponseMessageV2($id,$result=null,?object $error=null): RequestMessage{
256256
if(!is_null($result) && !is_null($error)){
257257
throw new JSONRPCException('[V2] Only one property "result" or "error" can be non null.');
258258
}
259259
if(!is_object($error) && !is_null($error)){
260-
throw new JSONRPCException('[V1] The "error" property in request MUST be an object or null.');
260+
throw new JSONRPCException('[V2] The "error" property in request MUST be an object or null.');
261261
}
262262
$arr = [
263263
'jsonrpc' => '2.0',
264264
'id' => $id,
265265
];
266266
if(!is_null($error)){
267+
if(!property_exists($error,'code')){
268+
throw new JSONRPCException('[V2] The error object MUST have a "code" property.');
269+
}
270+
if(!property_exists($error,'message')){
271+
throw new JSONRPCException('[V2] The error object MUST have a "message" property.');
272+
}
273+
if(!is_int($error->code)){
274+
throw new JSONRPCException('[V2] The "code" property of the error object MUST be an integer.');
275+
}
276+
if(!is_string($error->message)){
277+
throw new JSONRPCException('[V2] The "message" property of the error object MUST be a string.');
278+
}
267279
$arr['error'] = $error;
268280
}else{
269281
$arr['result'] = $result;

tests/MessageTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ public function testIsVersion2(){
409409
$this->assertTrue(Message::createResponseMessageV2(123,'myMethod')->isVersion2());
410410
}
411411

412+
/**
413+
* @return void
414+
* @throws JSONRPCException
415+
*/
416+
public function testCreateRequestMessageV2WithIdAndMethodAndParamsFalse(){
417+
$this->expectException(JSONRPCException::class);
418+
$this->expectExceptionMessage('[V2] The "params" property in request MUST be an object, array or null.');
419+
420+
Message::createRequestMessageV2(123,'abc',false);
421+
}
422+
412423
/**
413424
* @return void
414425
* @throws JSONRPCException
@@ -431,6 +442,18 @@ public function testCreateResponseMessageV1WithErrorFalse(){
431442
Message::createResponseMessageV1(123,null,false);
432443
}
433444

445+
/**
446+
* @return void
447+
* @throws JSONRPCException
448+
*/
449+
public function testCreateRequestOrNotificationV2(){
450+
$this->assertInstanceOf(RequestMessage::class,Message::createRequestMessageV2(123,'myMethod',[]));
451+
$this->assertInstanceOf(RequestMessage::class,Message::createRequestMessageV2(123,'myMethod',(object) []));
452+
453+
$this->assertInstanceOf(NotificationMessage::class,Message::createNotificationMessageV2('myMethod',[]));
454+
$this->assertInstanceOf(NotificationMessage::class,Message::createNotificationMessageV2('myMethod',(object) []));
455+
}
456+
434457
public function testToObject(){
435458
$this->assertEquals((object) ['id'=>123,'method'=>'getMethod','params'=>['param1','param2']],Message::createRequestMessageV1(123,'getMethod',['param1','param2'])->toObject());
436459
}

0 commit comments

Comments
 (0)