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

Commit

Permalink
Merge branch 'feature/5933' into develop
Browse files Browse the repository at this point in the history
Close #5933
  • Loading branch information
weierophinney committed Mar 7, 2014
2 parents 2f6d4a8 + e7e3eb0 commit 8ee3591
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
34 changes: 14 additions & 20 deletions library/Zend/Json/Server/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,11 @@ class Error
const ERROR_INTERNAL = -32603;
const ERROR_OTHER = -32000;

/**
* Allowed error codes
* @var array
*/
protected $allowedCodes = array(
self::ERROR_PARSE,
self::ERROR_INVALID_REQUEST,
self::ERROR_INVALID_METHOD,
self::ERROR_INVALID_PARAMS,
self::ERROR_INTERNAL,
self::ERROR_OTHER,
);

/**
* Current code
* @var int
*/
protected $code = -32000;
protected $code = self::ERROR_OTHER;

/**
* Error data
Expand All @@ -56,29 +43,36 @@ class Error
* @param int $code
* @param mixed $data
*/
public function __construct($message = null, $code = -32000, $data = null)
public function __construct($message = null, $code = self::ERROR_OTHER, $data = null)
{
$this->setMessage($message)
->setCode($code)
->setData($data);
}

/**
* Set error code
* Set error code.
*
* If the error code is 0, it will be set to -32000 (ERROR_OTHER).
*
* @param int $code
* @return \Zend\Json\Server\Error
*/
public function setCode($code)
{
if (!is_scalar($code)) {
if (!is_scalar($code) || is_bool($code) || is_float($code)) {
return $this;
}

if (is_string($code) && !is_numeric($code)) {
return $this;
}

$code = (int) $code;
if (in_array($code, $this->allowedCodes)) {
$this->code = $code;
} elseif (in_array($code, range(-32099, -32000))) {

if (0 === $code) {
$this->code = self::ERROR_OTHER;
} else {
$this->code = $code;
}

Expand Down
20 changes: 19 additions & 1 deletion tests/ZendTest/Json/Server/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testSetCodeShouldCastToInteger()

public function testCodeShouldBeLimitedToStandardIntegers()
{
foreach (array(true, 'foo', array(), new \stdClass, 2.0, 25) as $code) {
foreach (array(null, true, 'foo', array(), new \stdClass, 2.0) as $code) {
$this->error->setCode($code);
$this->assertEquals(Server\Error::ERROR_OTHER, $this->error->getCode());
}
Expand All @@ -68,6 +68,24 @@ public function testCodeShouldAllowArbitraryAppErrorCodesInXmlRpcErrorCodeRange(
}
}

public function arbitraryErrorCodes()
{
return array(
'1000' => array(1000),
'404' => array(404),
'-3000' => array(-3000),
);
}

/**
* @dataProvider arbitraryErrorCodes
*/
public function testCodeShouldAllowArbitraryErrorCode($code)
{
$this->error->setCode($code);
$this->assertEquals($code, $this->error->getCode());
}

public function testMessageShouldBeNullByDefault()
{
$this->assertNull($this->error->getMessage());
Expand Down

0 comments on commit 8ee3591

Please sign in to comment.