Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit cc6e408

Browse files
committed
Merge pull request zendframework/zendframework#5933 from gkralik/fix/json-rpc-error-codes
Allow arbitrary error codes in JSON RPC server
190 parents d14a7fd + d3b81bc + e4898d7 + 9c57882 + 3c03bad + 87df75a + 89e1265 + 5bc0027 + 83fd9fa + 3844e27 + a2b3a39 + cbb5bf2 + 3ef3347 + bc77506 + e5e4203 + 2a10a27 + 8ba934c + 23ca50c + fd53025 + d95e884 + fb9734f + c781722 + a76fdf6 + 7f04465 + 3bc62a9 + f574eb9 + 52d314b + bada1dc + 842e034 + b44331a + 8d1b4e9 + 59d10c2 + e630f51 + 96f4abb + bfcf88b + e2e6ee0 + bd53446 + 62fffaa + 075b3a5 + dfc876b + 2056187 + ccaac9b + 5088038 + e4fa7ad + efc7cf5 + 0f33a2d + a3589c7 + 3ddf1a3 + d86b089 + 491f302 + c0efd18 + f9e572c + 3077af2 + a811b75 + 585f923 + 9b385ff + ec1f853 + b1a5d58 + a1cf97e + c1b8314 + 1482b41 + 7d80b85 + 869bbf8 + 403ce8d + 008804b + 87522da + d31ec42 + 2f2a15a + 585cc82 + a9438e2 + d59be1f + 643e2df + a49e8f2 + 08fd26d + b52d5dc + de5abc1 + 48f2d19 + 34ca3a0 + 5169094 + 25abd15 + baa09a1 + 355680a + b2b0d91 + 6986810 + 7b1513c + 6f9a6cc + 39a1c1e + 92dbdb3 + ac3a5aa + ee15c3a + 67f414a + 49138fb + a4ebc7b + 6b11637 + f128e27 + 41746af + cc083fe + 3d54828 + ccb0392 + a8ab5b0 + 1128b0c + b5ca6d2 + 5ef536e + 6878fa7 + 66a7225 + 869024d + 82f5ac4 + a831196 + dde75d7 + 9aba77c + 8474496 + 7c3bb76 + ad8e418 + a262925 + ee212be + 7f01229 + 42758bb + e91fa40 + 21394b2 + c56da7c + 9327465 + 95794f5 + 86491b5 + b24dc4f + 295467a + dc56d1e + d4c89e8 + ddd7418 + 8d06baa + ba834b9 + 292f809 + d235382 + 2b8ddfb + a90e617 + 8897e13 + 88b1391 + dc0450d + 9fa0112 + 7b9adef + 8010d61 + 8aa284d + d94ef71 + a3e5e8a + 6601fe6 + 6290724 + 5e7c801 + d74f16a + f3c55cf + f7787b1 + e1c3e3c + 88e1629 + 3314d9a + 09dfc28 + 6eac5f2 + aa1a1a6 + c602a92 + 9275250 + 11d2caa + d3c8400 + 295642a + bcf5cb7 + 1f38df6 + 5ea4665 + c66b1db + 7869e69 + 24907b1 + 0e90be1 + b2f6310 + 4f9fe6f + d1a2e86 + 5f479d5 + fe40a9c + f764dfe + 2c1484c + bfa303a + 028676a + 37aa230 + f51739a + db6575f + 9341798 + 8aa8bae + 09de2cd + fe5aee0 + a593f5f + c8eb1ab + 11555dc + c361f54 + 39e7f86 + 349c4aa + 3872a2e commit cc6e408

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/Server/Error.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,11 @@ class Error
1818
const ERROR_INTERNAL = -32603;
1919
const ERROR_OTHER = -32000;
2020

21-
/**
22-
* Allowed error codes
23-
* @var array
24-
*/
25-
protected $allowedCodes = array(
26-
self::ERROR_PARSE,
27-
self::ERROR_INVALID_REQUEST,
28-
self::ERROR_INVALID_METHOD,
29-
self::ERROR_INVALID_PARAMS,
30-
self::ERROR_INTERNAL,
31-
self::ERROR_OTHER,
32-
);
33-
3421
/**
3522
* Current code
3623
* @var int
3724
*/
38-
protected $code = -32000;
25+
protected $code = self::ERROR_OTHER;
3926

4027
/**
4128
* Error data
@@ -56,11 +43,10 @@ class Error
5643
* @param int $code
5744
* @param mixed $data
5845
*/
59-
public function __construct($message = null, $code = -32000, $data = null)
60-
{
46+
public function __construct($message = null, $code = self::ERROR_OTHER, $data = null) {
6147
$this->setMessage($message)
62-
->setCode($code)
63-
->setData($data);
48+
->setCode($code)
49+
->setData($data);
6450
}
6551

6652
/**
@@ -71,17 +57,17 @@ public function __construct($message = null, $code = -32000, $data = null)
7157
*/
7258
public function setCode($code)
7359
{
74-
if (!is_scalar($code)) {
60+
if (!is_scalar($code) || is_bool($code) || is_float($code)) {
7561
return $this;
7662
}
7763

78-
$code = (int) $code;
79-
if (in_array($code, $this->allowedCodes)) {
80-
$this->code = $code;
81-
} elseif (in_array($code, range(-32099, -32000))) {
82-
$this->code = $code;
64+
if (is_string($code) && !is_numeric($code)) {
65+
return $this;
8366
}
8467

68+
$code = (int) $code;
69+
$this->code = $code;
70+
8571
return $this;
8672
}
8773

test/Server/ErrorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testSetCodeShouldCastToInteger()
5454

5555
public function testCodeShouldBeLimitedToStandardIntegers()
5656
{
57-
foreach (array(true, 'foo', array(), new \stdClass, 2.0, 25) as $code) {
57+
foreach (array(null, true, 'foo', array(), new \stdClass, 2.0) as $code) {
5858
$this->error->setCode($code);
5959
$this->assertEquals(Server\Error::ERROR_OTHER, $this->error->getCode());
6060
}
@@ -68,6 +68,14 @@ public function testCodeShouldAllowArbitraryAppErrorCodesInXmlRpcErrorCodeRange(
6868
}
6969
}
7070

71+
public function testCodeShouldAllowArbitraryErrorCode()
72+
{
73+
foreach(array(1000, 404, -3000) as $code) {
74+
$this->error->setCode($code);
75+
$this->assertEquals($code, $this->error->getCode());
76+
}
77+
}
78+
7179
public function testMessageShouldBeNullByDefault()
7280
{
7381
$this->assertNull($this->error->getMessage());

0 commit comments

Comments
 (0)