Skip to content

Commit a94d2b2

Browse files
committed
Merge pull request #26 from TSchuermans/feature/rpc-error-data
Added JSON-RPC 2.0 error data to response
2 parents 7e0dc22 + f39fbc1 commit a94d2b2

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/Message/Response.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public function getRpcErrorMessage()
3939
return isset($error['message']) ? $error['message'] : null;
4040
}
4141

42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function getRpcErrorData()
46+
{
47+
$error = $this->getFieldFromBody('error');
48+
49+
return isset($error['data']) ? $error['data'] : null;
50+
}
51+
4252
/**
4353
* {@inheritdoc}
4454
*/

src/Message/ResponseInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function getRpcErrorCode();
2828
*/
2929
public function getRpcErrorMessage();
3030

31+
/**
32+
* @return mixed
33+
*/
34+
public function getRpcErrorData();
35+
3136
/**
3237
* @return mixed
3338
*/

test/functional/RequestFunctionalTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testConcatRequest()
4848
$this->assertEquals($id, $response->getRpcId());
4949
$this->assertEquals(null, $response->getRpcErrorCode());
5050
$this->assertEquals(null, $response->getRpcErrorMessage());
51+
$this->assertEquals(null, $response->getRpcErrorData());
5152
}
5253

5354
public function testConcatAsyncRequest()
@@ -69,6 +70,7 @@ public function testConcatAsyncRequest()
6970
$this->assertEquals($id, $response->getRpcId());
7071
$this->assertEquals(null, $response->getRpcErrorCode());
7172
$this->assertEquals(null, $response->getRpcErrorMessage());
73+
$this->assertEquals(null, $response->getRpcErrorData());
7274
});
7375
}
7476

@@ -90,6 +92,7 @@ public function testSumRequest()
9092
$this->assertEquals($id, $response->getRpcId());
9193
$this->assertEquals(null, $response->getRpcErrorCode());
9294
$this->assertEquals(null, $response->getRpcErrorMessage());
95+
$this->assertEquals(null, $response->getRpcErrorData());
9396
}
9497

9598
public function testSumAsyncRequest()
@@ -111,6 +114,7 @@ public function testSumAsyncRequest()
111114
$this->assertEquals($id, $response->getRpcId());
112115
$this->assertEquals(null, $response->getRpcErrorCode());
113116
$this->assertEquals(null, $response->getRpcErrorMessage());
117+
$this->assertEquals(null, $response->getRpcErrorData());
114118
});
115119
}
116120

@@ -131,6 +135,7 @@ public function testFooRequest()
131135
$this->assertEquals($id, $response->getRpcId());
132136
$this->assertEquals(null, $response->getRpcErrorCode());
133137
$this->assertEquals(null, $response->getRpcErrorMessage());
138+
$this->assertEquals(null, $response->getRpcErrorData());
134139
}
135140

136141
public function testFooAsyncRequest()
@@ -151,6 +156,7 @@ public function testFooAsyncRequest()
151156
$this->assertEquals($id, $response->getRpcId());
152157
$this->assertEquals(null, $response->getRpcErrorCode());
153158
$this->assertEquals(null, $response->getRpcErrorMessage());
159+
$this->assertEquals(null, $response->getRpcErrorData());
154160
});
155161
}
156162

@@ -171,6 +177,7 @@ public function testBarRequest()
171177
$this->assertEquals($id, $response->getRpcId());
172178
$this->assertTrue(is_int($response->getRpcErrorCode()));
173179
$this->assertTrue(is_string($response->getRpcErrorMessage()));
180+
$this->assertEquals(null, $response->getRpcErrorData());
174181
}
175182

176183
public function testBarAsyncRequest()
@@ -191,6 +198,7 @@ public function testBarAsyncRequest()
191198
$this->assertEquals($id, $response->getRpcId());
192199
$this->assertTrue(is_int($response->getRpcErrorCode()));
193200
$this->assertTrue(is_string($response->getRpcErrorMessage()));
201+
$this->assertEquals(null, $response->getRpcErrorData());
194202
});
195203
}
196204

test/unit/Message/ResponseTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ public function testGetRpcErrorMessageIsNull()
7373
{
7474
$response = new Response(200);
7575

76-
$this->assertNull($response->getRpcErrorCode());
76+
$this->assertNull($response->getRpcErrorMessage());
77+
}
78+
79+
public function testGetRpcErrorData()
80+
{
81+
$response = new Response(200, [], json_encode([
82+
'error' => ['data' => array()]
83+
]));
84+
85+
$this->assertEquals(array(), $response->getRpcErrorData());
86+
}
87+
88+
public function testGetRpcErrorDataIsNull()
89+
{
90+
$response = new Response(200);
91+
92+
$this->assertNull($response->getRpcErrorData());
7793
}
7894

7995
public function testGetRpcResult()

0 commit comments

Comments
 (0)