1

I am currently looking into implementing some JSON RPC 2.0 responses in my code. However, I'm a little unclear on what are the standard practices of using it:

1) When the user sends a request with invalid parameters, should I just return verbatim default error message

 {"jsonrpc": "2.0", "error": {"code": -32602, "message": "  Invalid params"}, "id": "1"}

Or can the message be more specific, like:

 {"jsonrpc": "2.0", "error": {"code": -32602, "message": "  Invalid params: invalid username"}, "id": "1"}

Or should such custom messages have their own error code?

2) If the user say, requests data from the database and the response is "data is not present", as in we encountered no errors but still didn't return anything, should that be returned as a JSON RPC error, or should it be more of response indicating data wasn't found? In other words, is the convention in JSON RPC to use errors as normal return conditions like in Google Go, or is it more akin to "something really messed up" panics?

ThePiachu
  • 8,695
  • 17
  • 65
  • 94

1 Answers1

4
  1. According to the specification (http://www.jsonrpc.org/specification#error_object) you must use the optional property data for your additional information about the error, so, in your case the response must be:

{"jsonrpc": "2.0", "error": {"code": -32602, "message": " Invalid params", "data":"invalid username"}, "id": "1"}

You can create your own personal error codes in the range -32000 to -32099, but I would do it only when necessary, that is, unless your client application should behave in this case ("invalid username") different than in any other -32602 case.

  1. That is up to you. It is a design question with a broader scope than JSON-RCP. You can find some opinions at this post: when-to-throw-an-exception
Community
  • 1
  • 1
idelvall
  • 1,536
  • 15
  • 25