Skip to content

Commit

Permalink
added error.data support for JSON-RPC 2.0 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanbem authored Aug 8, 2016
1 parent 2236ca9 commit 58e2915
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
22 changes: 17 additions & 5 deletions src/Josser/Exception/RpcFaultException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@

namespace Josser\Exception;

use Josser\Exception\JosserException;

/**
* RPC fault exception.
*
* @author Alan Gabriel Bem <[email protected]>
*/
class RpcFaultException extends \RuntimeException implements JosserException
{
public function __construct($message, $code = null, $previous = null)
/**
* @var mixed
*/
private $data;

public function __construct($message, $code = 0, $data = null, \Exception $previous = null)
{
$message = 'RPC fault: ' . $message;
$this->data = $data;

parent::__construct($message, $code, $previous);
}
}

/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
}
1 change: 1 addition & 0 deletions src/Josser/Protocol/JsonRpc1.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function createResponse($dto)
$this->validateResponseDataTransferObject($dto);

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

Expand Down
9 changes: 8 additions & 1 deletion src/Josser/Protocol/JsonRpc2.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public function createResponse($dto)
$this->validateResponseDataTransferObject($dto);

if(isset($dto['error'])) {
throw new RpcFaultException($dto['error']['message'], $dto['error']['code']);
$message = $dto['error']['message'];
$code = $dto['error']['code'];
$data = null;
if (true === isset($dto['error']['data'])) {
$data = $dto['error']['data'];
}
throw new RpcFaultException($message, $code, $data);
}

$result = $dto['result'];
Expand Down Expand Up @@ -257,6 +263,7 @@ private function validateResponseDataTransferObjectError($rpcError)
throw new InvalidResponseException($error);
}

// TODO: validate that error object has only code, message and ?data fields
// TODO: validate optional 'data' attribute
}
}
50 changes: 50 additions & 0 deletions tests/Josser/Tests/Exception/RpcFaultExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Josser package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Josser\Tests\Exception;

use Josser\Exception\RpcFaultException;
use Josser\Tests\TestCase as JosserTestCase;

/**
* Test class for \Josser\Exception\RpcFaultException
*/
class RpcFaultExceptionTest extends JosserTestCase
{
public function testException()
{
$e = new RpcFaultException('message');

$this->assertEquals('message', $e->getMessage());
$this->assertEquals(0, $e->getCode());
$this->assertEquals(null, $e->getData());

$e = new RpcFaultException('message', 100);

$this->assertEquals('message', $e->getMessage());
$this->assertEquals(100, $e->getCode());
$this->assertEquals(null, $e->getData());

$e = new RpcFaultException('message', 100, null);

$this->assertEquals('message', $e->getMessage());
$this->assertEquals(100, $e->getCode());
$this->assertEquals(null, $e->getData());

$previous = new \Exception;
$e = new RpcFaultException('message', 100, null, $previous);

$this->assertEquals('message', $e->getMessage());
$this->assertEquals(100, $e->getCode());
$this->assertEquals(null, $e->getData());
$this->assertSame($previous, $e->getPrevious());
}
}
25 changes: 19 additions & 6 deletions tests/Josser/Tests/Protocol/JsonRpc2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Josser\Tests;

use Josser\Exception\RpcFaultException;
use Josser\Tests\TestCase as JosserTestCase;
use Josser\Protocol\JsonRpc2;
use Josser\Client\Request\Request;
Expand Down Expand Up @@ -97,15 +98,23 @@ public function testCreatingResponseFromValidDtoWithoutRpcError($responseDataTra
* Test protocols' response objects factory and its RPC fault detection if invalid DTO is provided.
*
* @param mixed $responseDataTransferObject
* @param mixed $message
* @param mixed $code
* @param mixed $data
* @return void
*
* @dataProvider validResponseDTOsWithRpcErrorDataProvider
*/
public function testCreatingResponseFromValidDtoWithRpcError($responseDataTransferObject)
public function testCreatingResponseFromValidDtoWithRpcError($responseDataTransferObject, $message, $code, $data)
{
$this->setExpectedException('Josser\Exception\RpcFaultException');

$this->protocol->createResponse($responseDataTransferObject);
$this->setExpectedException('Josser\Exception\RpcFaultException', $message, $code);

try {
$this->protocol->createResponse($responseDataTransferObject);
} catch (RpcFaultException $e) {
$this->assertEquals($data, $e->getData());
throw $e;
}
}

/**
Expand Down Expand Up @@ -231,7 +240,11 @@ public function validResponseDTOsWithoutRpcErrorDataProvider()
public function validResponseDTOsWithRpcErrorDataProvider()
{
return array(
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000), 'id' => 1)), // RPC error
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000), 'id' => 1), 'Error message', 1000, null),
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => 1), 'id' => 1), 'Error message', 1000, 1),
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => '1'), 'id' => 1), 'Error message', 1000, '1'),
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => []), 'id' => 1), 'Error message', 1000, []),
array(array('jsonrpc' => '2.0', 'error' => array('message' => 'Error message', 'code' => 1000, 'data' => ['error' => 'error']), 'id' => 1), 'Error message', 1000, ['error' => 'error']),
);
}

Expand Down Expand Up @@ -366,4 +379,4 @@ public function requestsAndNotificationsDataProvider()
array(new Notification('system.exit', array()), true),
);
}
}
}

0 comments on commit 58e2915

Please sign in to comment.