Permalink
Browse files

fixed error code handling

  • Loading branch information...
1 parent cb70cdd commit 3429536a706eb0ab63a92092bf0e27d51ede92cb @BrianBelhumeur committed Jan 12, 2013
Showing with 276 additions and 203 deletions.
  1. +27 −3 README.md
  2. +212 −200 lib/jsonrpc.js
  3. +37 −0 test/testServer/public/javascripts/unitTests.js
View
@@ -1,7 +1,11 @@
+### Install
+
node-express-json-rpc2 is a [JSON-RPC version 2 spec](http://www.jsonrpc.org/specification)-compliant handler middleware for the Express library on [node.js](http://nodejs.org). It is designed for use within routes. You can install it via:
npm install node-express-json-rpc2
+### Setup
+
To use, simply include node-express-JSON-RPC2 in your configure/use statements before app.router:
var express = require('express'),
@@ -14,7 +18,9 @@ To use, simply include node-express-JSON-RPC2 in your configure/use statements b
app.use( app.router );
});
-Then wihin a route, use res.rpc() to handle a given method. The first argument is the method name and the second argument is the function that will be invoked to handle the request.
+### Use
+
+Within a route, use res.rpc() to handle a given method. The first argument is the method name and the second argument is the function that will be invoked to handle the request.
The first argument passed to the invocation function is the value of the "params" property from the RPC request. Unless the request is a notification, the second argument will be a function to handle the response. The response function will automatically wrap the data passed to it in the RPC response object containing the request ID and jsonrpc property.
@@ -44,12 +50,30 @@ The first argument passed to the invocation function is the value of the "params
});
});
-The middleware provides standard error codes and messages defined by the [JSON-RPC version 2 spec](http://www.jsonrpc.org/specification)
+### Success Responses
+
+To respond successfully, simpy pass in an object with a key of 'result' and whatever value you want to respond with:
+
+ respond({ result: [ 'Success!' ] });
+
+And your response will go out as:
+
+ {
+ "jsonrpc": "2.0",
+ "result": [ "Success!" ],
+ "id": XXX // id will match the ID of the request
+ }
-You can reference error codes by using built-in variable names. For example, you may:
+### Error Responses
+
+The middleware provides standard error codes and messages defined by the [JSON-RPC version 2 spec](http://www.jsonrpc.org/specification). You can reference error codes by using built-in variable names. For example, you may respond with just the error code:
respond(jsonrpc.INVALID_REQUEST);
+or with an error object:
+
+ respond({ error: { code: jsonrpc.INVALID_REQUEST, message: "Invalid Request" } });
+
And your response will go out as:
{
Oops, something went wrong.

0 comments on commit 3429536

Please sign in to comment.