Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Issue4817 #4818

Closed
wants to merge 6 commits into from

3 participants

@tylkomat

Fix for #4817

@mwillbanks
Collaborator

Could you please provide some more detail in this PR? I did look at the other issue but some sources and other items would be great :)

@tylkomat

Ah sure. Following this specification www.jsonrpc.org/specification the Error Code for a parse error should be -32700. This error should be sent when the JSON sent to the service is malformed. An example from the same page:
A request {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz] should create this response: {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}

Before my PR the JSON parser just threw an unhandled exception which in this case is not desired and should be handled. Like the already existing isMethodError function I created an ìsParseError function so the Zend\Json\Server\Server can check in the Zend\Json\Server\Request object for it and create a proper Zend\Json\Server\Response.

@weierophinney weierophinney closed this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4818'
Close #4818
Fixes #4817
6bc8d0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 12, 2013
  1. according to http://www.jsonrpc.org/specification#error_object the er…

    Matthias Tylkowski authored
    …ror code for parse error is -32700
  2. catch exception when parsing malformed JSON and set property

    Matthias Tylkowski authored
  3. check for parse error property on request

    Matthias Tylkowski authored
  4. added test case, formatting

    Matthias Tylkowski authored
  5. comment added

    Matthias Tylkowski authored
  6. fixed ErrorTest.php and put real value for parse error

    Matthias Tylkowski authored
This page is out of date. Refresh to see the latest.
View
2  library/Zend/Json/Server/Error.php
@@ -11,7 +11,7 @@
class Error
{
- const ERROR_PARSE = -32768;
+ const ERROR_PARSE = -32700;
const ERROR_INVALID_REQUEST = -32600;
const ERROR_INVALID_METHOD = -32601;
const ERROR_INVALID_PARAMS = -32602;
View
24 library/Zend/Json/Server/Request.php
@@ -29,6 +29,12 @@ class Request
protected $isMethodError = false;
/**
+ * Flag
+ * @var bool
+ */
+ protected $isParseError = false;
+
+ /**
* Requested method
* @var string
*/
@@ -179,6 +185,16 @@ public function isMethodError()
}
/**
+ * Was a malformed JSON provided?
+ *
+ * @return bool
+ */
+ public function isParseError()
+ {
+ return $this->isParseError;
+ }
+
+ /**
* Set request identifier
*
* @param mixed $name
@@ -234,8 +250,12 @@ public function getVersion()
*/
public function loadJson($json)
{
- $options = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
- $this->setOptions($options);
+ try {
+ $options = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
+ $this->setOptions($options);
+ } catch(\Exception $e) {
+ $this->isParseError = true;
+ }
}
/**
View
4 library/Zend/Json/Server/Server.php
@@ -483,6 +483,10 @@ protected function _handle()
{
$request = $this->getRequest();
+ if($request->isParseError()){
+ return $this->fault('Parse error', Error::ERROR_PARSE);
+ }
+
if (!$request->isMethodError() && (null === $request->getMethod())) {
return $this->fault('Invalid Request', Error::ERROR_INVALID_REQUEST);
}
View
4 tests/ZendTest/Json/Server/ErrorTest.php
@@ -52,8 +52,8 @@ public function testCodeShouldBeErrOtherByDefault()
public function testSetCodeShouldCastToInteger()
{
- $this->error->setCode('-32768');
- $this->assertEquals(-32768, $this->error->getCode());
+ $this->error->setCode('-32700');
+ $this->assertEquals(-32700, $this->error->getCode());
}
public function testCodeShouldBeLimitedToStandardIntegers()
View
7 tests/ZendTest/Json/Server/RequestTest.php
@@ -233,6 +233,13 @@ public function testMethodNamesShouldAllowDotNamespacing()
$this->assertEquals('foo.bar', $this->request->getMethod());
}
+ public function testIsParseErrorSetOnMalformedJson()
+ {
+ $testJson = '{"id":1, "method": "test", "params:"[1,2,3]}';
+ $this->request->loadJson($testJson);
+ $this->assertTrue($this->request->isParseError());
+ }
+
public function getOptions()
{
return array(
Something went wrong with that request. Please try again.