khaintt / android-json-rpc

Automatically exported from code.google.com/p/android-json-rpc
0 stars 0 forks source link

Errors from -32000 to -32099 shoudn't result in Exception #10

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Make a callJSONObject to a WS which returns a error object like the following

{
    "error": {
        "code": -32001,
        "message": "Callback Service RPC Error code",
        "data": {}
    },
    "id": "1234",
    "jsonrpc": "2.0"
}

What is the expected output? What do you see instead?

The expected output should be a JSONObject with the code, message and data 
fields. What I see instead it´s a JSONRPCException because the result is null.

What version of the product are you using? On what operating system?

version 0.3.4

Please provide any additional information below.

The JSON-RPC Specification states that errors from -32000 to -32099 are 
reserved for implementation-defined server-errors. They shouldn't raise a 
Exception.

Original issue reported on code.google.com by oneil...@gmail.com on 17 Feb 2014 at 2:02

Attachments:

GoogleCodeExporter commented 9 years ago
This gets solved with this snippet

On JSONRPCHttpClient.java @ doJSONRequest method (line 117)

if (!jsonError.equals(null)) {
                    JSONObject errorObj = jsonResponse.getJSONObject("error");
                    int code = errorObj.getInt("code");
                    if (code >= -32099 && code <= -32000)
                            return jsonResponse;
                    else
                        throw new JSONRPCException(jsonResponse.get("error"));
                }

And handled in JSONRPCClient.java @ callJSONObject method (line 524)

try {
                if (response.has("result")) {
                    return new JSONObject(response.getString("result"));
                } else{
                    return new JSONObject(response.getString("error"));
                }

            } catch (NumberFormatException e1) {
                throw new JSONRPCException("Cannot convert result to JSONObject", e);
            } catch (JSONException e1) {
                throw new JSONRPCException("Cannot convert result to JSONObject", e);
            }

Original comment by oneil...@gmail.com on 17 Feb 2014 at 2:47