Browse files

Changed package.json, how client handles HTTP errors and other tidyings

  • Loading branch information...
1 parent 2f846f7 commit 0269fca200385458f999c4ed3fe7ac87ff0d7749 @tedeh committed Jun 30, 2012
Showing with 44 additions and 22 deletions.
  1. +2 −1 Makefile
  2. +16 −9 lib/client/http.js
  3. +1 −1 lib/client/jquery.js
  4. +1 −1 lib/server/fork.js
  5. +5 −1 lib/server/http.js
  6. +3 −1 lib/server/https.js
  7. +10 −2 lib/server/middleware.js
  8. +5 −5 lib/utils.js
  9. +1 −1 package.json
View
3 Makefile
@@ -1,7 +1,8 @@
+SOURCE ?= lib/client/jquery.js
+
test:
@./node_modules/.bin/mocha
compile:
- @node ./bin/builder.js
.PHONY: compile test
View
25 lib/client/http.js
@@ -46,15 +46,23 @@ HttpClient.prototype._request = function(request, callback) {
res.setEncoding(options.encoding);
res.on('data', function(chunk) { data += chunk; });
res.on('end', function() {
- // empty reply
- if(!data || typeof(data) !== 'string') return callback();
+ // assume we have an error
+ if(res.statusCode < 200 || res.statusCode >= 300) {
+ // assume the server gave the reason in the body
+ var err = new Error(data);
+ err.code = res.statusCode;
+ callback(err);
+ } else {
+ // empty reply
+ if(!data || typeof(data) !== 'string') return callback();
- try {
- var response = JSON.parse(data, options.reviver);
- } catch(exception) {
- return callback(exception);
+ try {
+ var response = JSON.parse(data, options.reviver);
+ } catch(exception) {
+ return callback(exception);
+ }
+ callback(null, response);
}
- callback(null, response);
});
});
@@ -63,6 +71,5 @@ HttpClient.prototype._request = function(request, callback) {
req.abort();
});
- req.write(body);
- req.end();
+ req.end(body);
};
View
2 lib/client/jquery.js
@@ -1,4 +1,4 @@
-;(function($) {
+;(function($, undefined) {
var ClientJquery = function(options) {
if(!(this instanceof ClientJquery)) return new ClientJquery(options);
View
2 lib/server/fork.js
@@ -5,7 +5,7 @@ var JaysonServer = require('./index');
/**
* Constructor for a Jayson JSON-RPC Fork Server
- * @class JSON-RPC 2.0 Fork Server
+ * @class Jayson JSON-RPC Fork Server
* @param {String} path The path that contains the fork
* @param {Object} options
* @returns {ForkServer}
View
6 lib/server/http.js
@@ -5,11 +5,15 @@ var utils = require('../utils');
* Constructor for a Jayson HTTP server
* @class Jayson JSON-RPC HTTP Server
* @extends require('http').Server
+ * @param {Server} server Server instance
+ * @param {Object} [options] Options for this instance
* @return {HttpServer}
* @api public
*/
-var HttpServer = module.exports = function(server, options) {
+var HttpServer = function(server, options) {
if(!(this instanceof HttpServer)) return new HttpServer(server, options);
http.Server.call(this, utils.httpRequestWrapper(server, options));
};
utils.inherits(HttpServer, http.Server);
+
+module.exports = HttpServer;
View
4 lib/server/https.js
@@ -5,7 +5,9 @@ var utils = require('../utils');
* Constructor for a Jayson HTTPS server
* @class Jayson JSON-RPC HTTPS Server
* @extends require('https').Server
- * @return {HttpServer}
+ * @param {Server} server Server instance
+ * @param {Object} [options] Options for this instance
+ * @return {HttpServers}
* @api public
*/
var HttpsServer = function(server, options) {
View
12 lib/server/middleware.js
@@ -1,6 +1,14 @@
var utils = require('../utils');
-var JaysonMiddleware = function(server, options) {
+/**
+ * Creates a Connect/Express compatible middleware bound to a Server
+ * @class Jayson JSON-RPC Middleware
+ * @param {Server} server Server instance
+ * @param {Object} [options] Options for this function
+ * @return {Function}
+ * @api public
+ */
+var Middleware = function(server, options) {
return function(req, res, next) {
var options = utils.merge(server.options, options || {});
@@ -42,4 +50,4 @@ var JaysonMiddleware = function(server, options) {
};
};
-module.exports = JaysonMiddleware;
+module.exports = Middleware;
View
10 lib/utils.js
@@ -118,14 +118,14 @@ Utils.httpRequestWrapper = function(server, options) {
options = Utils.merge(server.options, options || {});
// 405 method not allowed if not POST
- if(!Utils.isMethod(req, 'POST')) return error(405, { 'allow': 'POST' });
+ if(!Utils.isMethod(req, 'POST')) return error('Method not allowed', 405, { 'allow': 'POST' });
// 415 unsupported media type if content-type is not correct
- if(!Utils.isContentType(req, 'application/json')) return error(415);
+ if(!Utils.isContentType(req, 'application/json')) return error('Unsupported media type', 415);
Utils.parseBody(req, options.reviver, function(err, request) {
// parsing failed, 500 server error
- if(err) return error(500);
+ if(err) return error(err, 500);
server.call(request, function(error, success) {
var response = error || success;
@@ -150,9 +150,9 @@ Utils.httpRequestWrapper = function(server, options) {
});
// ends the request with an error code
- function error(code, headers) {
+ function error(err, code, headers) {
res.writeHead(code, headers || {});
- res.end();
+ res.end(String(err));
}
};
View
2 package.json
@@ -13,7 +13,7 @@
"bugs": "https://github.com/tedeh/jayson/issues",
"main": "index",
"engines": {
- "node": ">= 0.5.0 < 0.7.0"
+ "node": ">= 0.4.0"
},
"scripts": { "test": "make test" },
"dependencies": {

0 comments on commit 0269fca

Please sign in to comment.