Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed handler format to conform to Node.js norms.

Callbacks should always take an "err" argument first for asynchronous
error handling.

Callbacks should be passed in as the last parameter (this was already
the case).

The parameter array should be passed in as an array. -- Now, I had
quite some internal debate about this. It is nicer syntax-wise to have
the arguments come in as actual arguments, not as an array.

However, it does have the effect that the callback parameter moves
around depending on user-submitted data. So the function will have to
handle finding out which parameter is the callback, which is a lot
uglier and more error-prone than just passing in the arguments as an
array.

Finally, I've added an additional parameter "opt" which contains the
HTTP request object and the server object. This is useful as an
extensible way to deliver more features and options to the handler in
future versions of the library.

Here is an example handler with the new syntax:

server.expose('multiply', function (args, opt, callback) {
  var a = args[0],
      b = args[1];

  callback(null, a*b);
});
  • Loading branch information...
commit 4d7cfe6701af6f074e14184e4d31cf5dc4e08e62 1 parent baf3779
@justmoon justmoon authored
Showing with 16 additions and 6 deletions.
  1. +16 −6 src/jsonrpc.js
View
22 src/jsonrpc.js
@@ -193,14 +193,24 @@ Server.prototype.handlePOST = function(req, res) {
// Try to call the method, but intercept errors and call our
// onFailure handler.
var method = self.functions[decoded.method];
- var args = decoded.params.push(function(resp) {
- onSuccess(resp);
- });
+ var callback = function(err, result) {
+ if (err) {
+ onFailure(err);
+ } else {
+ onSuccess(result);
+ }
+ };
+
+ // Other various information we want to pass in for the handler to be
+ // able to access.
+ var opt = {
+ req: req,
+ server: self
+ };
try {
- method.apply(null, decoded.params);
- }
- catch(err) {
+ method.call(null, decoded.params, opt, callback);
+ } catch (err) {
return onFailure(err);
}
Please sign in to comment.
Something went wrong with that request. Please try again.