@@ -26,13 +26,13 @@ public function parseJsonRpcBody($rawBody) {
2626 try {
2727 $ parameters = Json::decode ($ rawBody , false );
2828 if (!$ parameters ) {
29- throw new Exception ('Not a valid JSON-RPC 2.0 request. ' , JSON_RPC_ERROR_PARSE );
29+ throw new Exception ('Could not parse JSON-RPC request body - empty result . ' , JSON_RPC_ERROR_REQUEST_INVALID );
3030 }
3131
3232 return $ parameters ;
3333 }
3434 catch (Exception $ e ) {
35- throw new Exception ('Not a valid JSON-RPC 2.0 request. ' , JSON_RPC_ERROR_PARSE );
35+ throw new Exception ('Could not parse JSON-RPC request. ' , JSON_RPC_ERROR_PARSE );
3636 }
3737 }
3838
@@ -43,7 +43,7 @@ public function parseJsonRpcBody($rawBody) {
4343 * @return bool|string
4444 */
4545 public function parseMethod ($ method ) {
46- if (!preg_match ('/^[\d\w_.]+/$ ' , $ method )) {
46+ if (!preg_match ('/^[\d\w_.]+$/ ' , $ method )) {
4747 return false ;
4848 }
4949
@@ -65,25 +65,21 @@ public function parseMethod($method) {
6565 */
6666 public function parseRequest ($ request ) {
6767 if (!isset ($ request ->jsonrpc ) || $ request ->jsonrpc !== '2.0 ' ) {
68- // TODO Customize error message to make the problem clear
69- throw new Exception ("The JSON sent is not a correct JSON-RPC request. " , JSON_RPC_ERROR_INVALID_REQUEST );
68+ throw new Exception ("The JSON sent is not a correct JSON-RPC request - missing or incorrect version. " , JSON_RPC_ERROR_REQUEST_INVALID );
7069 }
7170
7271 if (!isset ($ request ->method ) || !is_string ($ request ->method ) || (!$ route = $ this ->parseMethod ($ request ->method ))) {
73- throw new Exception ("The JSON sent is not a correct JSON-RPC request. " , JSON_RPC_ERROR_INVALID_REQUEST );
72+ throw new Exception ("The JSON sent is not a correct JSON-RPC request - missing or incorrect method . " , JSON_RPC_ERROR_REQUEST_INVALID );
7473 }
7574
7675 $ params = null ;
7776 if (isset ($ request ->params )) {
78- if (!is_array ($ request ->params )) {
79- throw new Exception ("The JSON sent is not a correct JSON-RPC request. " , JSON_RPC_ERROR_INVALID_REQUEST );
80- }
81- $ params = $ request ->params ;
77+ $ params = (array ) $ request ->params ;
8278 }
8379
8480 if (!isset ($ request ->id )) {
85- if (!is_scalar ($ request ->id )) {
86- throw new Exception ("The JSON sent is not a correct JSON-RPC request. " , JSON_RPC_ERROR_INVALID_REQUEST );
81+ if (!is_int ( $ request -> id ) && ! ctype_digit ($ request ->id )) {
82+ throw new Exception ("The JSON sent is not a correct JSON-RPC request - incorrect id . " , JSON_RPC_ERROR_INVALID_REQUEST );
8783 }
8884 }
8985
@@ -121,6 +117,7 @@ public function parseRequests($params) {
121117 }
122118 else {
123119 $ result = new Exception ("Error happened during request parsing. " , JSON_RPC_ERROR_INTERNAL );
120+ $ result ->data ['exception ' ] = $ exception ->getMessage ();
124121 }
125122 }
126123 $ results [] = $ result ;
@@ -152,7 +149,9 @@ public function executeRequest($request) {
152149 return $ exception ;
153150 }
154151 else {
155- return new Exception ('Internal error. ' , JSON_RPC_ERROR_INTERNAL );
152+ $ result = new Exception ('Internal error. ' , JSON_RPC_ERROR_INTERNAL );
153+ $ result ->data ['message ' ] = $ exception ->getMessage ();
154+ return $ result ;
156155 }
157156 }
158157 }
0 commit comments