Skip to content

Commit

Permalink
Add example for JSON-RPC returning an error object
Browse files Browse the repository at this point in the history
A real object could be used as well as a string here, but jsolait
as bundled stringifies the object when it raises a JSONRPCError
exception.
  • Loading branch information
Paul Hampson committed Nov 9, 2009
1 parent 908fea6 commit af707a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 19 additions & 1 deletion www/json_sample.html
Expand Up @@ -6,7 +6,7 @@
<script>

var serviceURL = "json_sample.yaws";
var methods = [ "test1" ];
var methods = [ "test1", "errortest" ];

var jsonrpc = imprt("jsonrpc");
var service = new jsonrpc.ServiceProxy(serviceURL, methods);
Expand All @@ -23,6 +23,17 @@
return false;
}

function errortest() {
try {
document.getElementById('failure').innerHTML =
"<PRE>" + service.errortest() + "</PRE>";
} catch(e) {
document.getElementById('failure').innerHTML =
"<PRE>" + e + "</PRE>";
}
return false;
}

</script>
<body>
<form action="" method="post" onSubmit="return test()">
Expand All @@ -38,5 +49,12 @@
<input type="submit" value="Do JSON-RPC call"/>
</p>
</form>
<form action="" method="post" onSubmit="return errortest()">
<p>
<input type="submit" value="Do JSON-RPC call expected to fail"/>
</p>
<div id="failure">
</div>
</form>
</body>
</html>
4 changes: 4 additions & 0 deletions www/json_sample.yaws
Expand Up @@ -16,6 +16,10 @@ out(A) ->



counter([{ip, IP}] = _State, {call, errortest, Value} = _Request, Session) ->
io:format("Request = ~p~n", [_Request]),
{ false, { error, "Expected failure" } };

counter([{ip, IP}] = _State, {call, test1, Value} = _Request, Session) ->
io:format("Request = ~p~n", [_Request]),
IPStr = io_lib:format("Client ip is ~p~n" , [ IP ]),
Expand Down

0 comments on commit af707a9

Please sign in to comment.