Skip to content

Commit 58e2915

Browse files
authored
added error.data support for JSON-RPC 2.0 (#1)
1 parent 2236ca9 commit 58e2915

File tree

5 files changed

+95
-12
lines changed

5 files changed

+95
-12
lines changed

src/Josser/Exception/RpcFaultException.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@
1111

1212
namespace Josser\Exception;
1313

14-
use Josser\Exception\JosserException;
15-
1614
/**
1715
* RPC fault exception.
1816
*
1917
* @author Alan Gabriel Bem <alan.bem@gmail.com>
2018
*/
2119
class RpcFaultException extends \RuntimeException implements JosserException
2220
{
23-
public function __construct($message, $code = null, $previous = null)
21+
/**
22+
* @var mixed
23+
*/
24+
private $data;
25+
26+
public function __construct($message, $code = 0, $data = null, \Exception $previous = null)
2427
{
25-
$message = 'RPC fault: ' . $message;
28+
$this->data = $data;
29+
2630
parent::__construct($message, $code, $previous);
2731
}
28-
}
32+
33+
/**
34+
* @return mixed
35+
*/
36+
public function getData()
37+
{
38+
return $this->data;
39+
}
40+
}

src/Josser/Protocol/JsonRpc1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function createResponse($dto)
4242
$this->validateResponseDataTransferObject($dto);
4343

4444
if(isset($dto['error'])) {
45+
// TODO: every protocol has its own exceptions, do not reuse exception between protocols
4546
throw new RpcFaultException($dto['error']['message']);
4647
}
4748

src/Josser/Protocol/JsonRpc2.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ public function createResponse($dto)
4242
$this->validateResponseDataTransferObject($dto);
4343

4444
if(isset($dto['error'])) {
45-
throw new RpcFaultException($dto['error']['message'], $dto['error']['code']);
45+
$message = $dto['error']['message'];
46+
$code = $dto['error']['code'];
47+
$data = null;
48+
if (true === isset($dto['error']['data'])) {
49+
$data = $dto['error']['data'];
50+
}
51+
throw new RpcFaultException($message, $code, $data);
4652
}
4753

4854
$result = $dto['result'];
@@ -257,6 +263,7 @@ private function validateResponseDataTransferObjectError($rpcError)
257263
throw new InvalidResponseException($error);
258264
}
259265

266+
// TODO: validate that error object has only code, message and ?data fields
260267
// TODO: validate optional 'data' attribute
261268
}
262269
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Josser package.
5+
*
6+
* (C) Alan Gabriel Bem <alan.bem@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Josser\Tests\Exception;
13+
14+
use Josser\Exception\RpcFaultException;
15+
use Josser\Tests\TestCase as JosserTestCase;
16+
17+
/**
18+
* Test class for \Josser\Exception\RpcFaultException
19+
*/
20+
class RpcFaultExceptionTest extends JosserTestCase
21+
{
22+
public function testException()
23+
{
24+
$e = new RpcFaultException('message');
25+
26+
$this->assertEquals('message', $e->getMessage());
27+
$this->assertEquals(0, $e->getCode());
28+
$this->assertEquals(null, $e->getData());
29+
30+
$e = new RpcFaultException('message', 100);
31+
32+
$this->assertEquals('message', $e->getMessage());
33+
$this->assertEquals(100, $e->getCode());
34+
$this->assertEquals(null, $e->getData());
35+
36+
$e = new RpcFaultException('message', 100, null);
37+
38+
$this->assertEquals('message', $e->getMessage());
39+
$this->assertEquals(100, $e->getCode());
40+
$this->assertEquals(null, $e->getData());
41+
42+
$previous = new \Exception;
43+
$e = new RpcFaultException('message', 100, null, $previous);
44+
45+
$this->assertEquals('message', $e->getMessage());
46+
$this->assertEquals(100, $e->getCode());
47+
$this->assertEquals(null, $e->getData());
48+
$this->assertSame($previous, $e->getPrevious());
49+
}
50+
}

tests/Josser/Tests/Protocol/JsonRpc2Test.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Josser\Tests;
1313

14+
use Josser\Exception\RpcFaultException;
1415
use Josser\Tests\TestCase as JosserTestCase;
1516
use Josser\Protocol\JsonRpc2;
1617
use Josser\Client\Request\Request;
@@ -97,15 +98,23 @@ public function testCreatingResponseFromValidDtoWithoutRpcError($responseDataTra
9798
* Test protocols' response objects factory and its RPC fault detection if invalid DTO is provided.
9899
*
99100
* @param mixed $responseDataTransferObject
101+
* @param mixed $message
102+
* @param mixed $code
103+
* @param mixed $data
100104
* @return void
101105
*
102106
* @dataProvider validResponseDTOsWithRpcErrorDataProvider
103107
*/
104-
public function testCreatingResponseFromValidDtoWithRpcError($responseDataTransferObject)
108+
public function testCreatingResponseFromValidDtoWithRpcError($responseDataTransferObject, $message, $code, $data)
105109
{
106-
$this->setExpectedException('Josser\Exception\RpcFaultException');
107-
108-
$this->protocol->createResponse($responseDataTransferObject);
110+
$this->setExpectedException('Josser\Exception\RpcFaultException', $message, $code);
111+
112+
try {
113+
$this->protocol->createResponse($responseDataTransferObject);
114+
} catch (RpcFaultException $e) {
115+
$this->assertEquals($data, $e->getData());
116+
throw $e;
117+
}
109118
}
110119

111120
/**
@@ -231,7 +240,11 @@ public function validResponseDTOsWithoutRpcErrorDataProvider()
231240
public function validResponseDTOsWithRpcErrorDataProvider()
232241
{
233242
return array(
234-
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000), 'id' => 1)), // RPC error
243+
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000), 'id' => 1), 'Error message', 1000, null),
244+
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => 1), 'id' => 1), 'Error message', 1000, 1),
245+
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => '1'), 'id' => 1), 'Error message', 1000, '1'),
246+
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => []), 'id' => 1), 'Error message', 1000, []),
247+
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => ['error' => 'error']), 'id' => 1), 'Error message', 1000, ['error' => 'error']),
235248
);
236249
}
237250

@@ -366,4 +379,4 @@ public function requestsAndNotificationsDataProvider()
366379
array(new Notification('system.exit', array()), true),
367380
);
368381
}
369-
}
382+
}

0 commit comments

Comments
 (0)