Just in case you ever wanted to develop a web-application in Erlang and Javascript, you probably stumbled upon JSON-RPC. The idea of JSON-RPC is as trivial as it sounds: You assemble a JSON object describing a remote procedure call, which usually consists of a method name, parameters and a unique id for asynchronous calls. This might look like the following:
{"version":"1.1","method":"login","id":2,"params":["myusername","mypassword"]}
The string representation of a JSON object will then be sent to our yaws-JSON-RPC-server. The documentation describes a simple case which is always successful in returning the requested result. A JSON-RPC reply looks like this:
{"result""id":2}
Using the recommended yaws_rpc module, our erlang program looks - in essence - somewhat like this:
out(A) -> ... yaws_rpc:handler_session(A2, {?MODULE, handler}). handler(..., {call, login, Params}, .....) -> ... {true, 0, Session, {response, true}}.
Now we assume, that every once in a while your server function fails internally, let's say due to an unstable database connection. Naturally we have to reply with an error in this case. The error could - for example - be indicated by a HTTP return code other than 200 (200=success). The handler function of our server code would then simply return the error like {error, "message", 500} instead of {true, ...} (the last line). Alternatively the error could be coded into the JSON-RPC reply like so:
{"id":6,"error":{"code":23,"message":"this and that"}}
Unfortunately this error reply is not easily determined by the handler's return value using the yaws_rpc module, unless it's been patched: yaws_rpc.erl-1.73.diff yaws_rpc_fixed.erl
After applying the patch, a return value of {jsonrpcerror, 23, "this and that"} should do the trick.