Skip to content

Commit

Permalink
Merge branch 'release/2.7.1' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Oct 28, 2014
2 parents 60ace53 + 9c6a30a commit 334cb05
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea/
*.seed
*.log
*.csv
Expand Down
45 changes: 43 additions & 2 deletions lib/http-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ var EventEmitter = require('events').EventEmitter
, inherits = util.inherits
, assert = require('assert')
, Dynamic = require('./dynamic')
, SUPPORTED_TYPES = ['json', 'application/javascript', 'text/javascript'];
, js2xmlparser = require('js2xmlparser')
, SUPPORTED_TYPES = [
'application/json', 'application/javascript', 'application/xml',
'text/javascript', 'text/xml',
'json', 'xml',
'*/*'
];

/**
* Create a new `HttpContext` with the given `options`.
Expand Down Expand Up @@ -186,7 +192,7 @@ function coerceAll(obj) {
Object.keys(obj).forEach(function (key) {
obj[key] = coerceAll(obj[key]);
});
}
}
break;
case 'array':
obj.map(function (o) {
Expand Down Expand Up @@ -262,13 +268,48 @@ HttpContext.prototype.done = function () {

if(dataExists) {
switch(accepts) {
case '*/*':
case 'application/json':
case 'json':
res.json(data);
break;
case 'application/javascript':
case 'text/javascript':
res.jsonp(data);
break;
case 'application/xml':
case 'text/xml':
case 'xml':
if (accepts == 'application/xml') {
res.header('Content-Type', 'application/xml');
} else {
res.header('Content-Type', 'text/xml');
}
if (data === null) {
res.header('Content-Length', '7');
res.end('<null/>');
} else {
try {
var input = data;
if (Object.prototype.toString.call(input) === '[object Array]') {
input = { result: data };
}
var xml = js2xmlparser('response', input, {
prettyPrinting: {
indentString: ' '
},
convertMap: {
'[object Date]': function (date) {
return date.toISOString();
}
}
});
res.send(xml);
} catch(e) {
res.send(500, e + '\n' + data);
}
}
break;
default:
// not acceptable
res.send(406);
Expand Down
55 changes: 36 additions & 19 deletions lib/rest-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ RestAdapter.prototype.connect = function(url) {
RestAdapter.prototype.invoke = function(method, ctorArgs, args, callback) {
assert(this.connection, 'Cannot invoke method without a connection. See RemoteObjects#connect().');
assert(typeof method === 'string', 'method is required when calling invoke()');

var lastArg = arguments[arguments.length - 1];
callback = typeof lastArg === 'function' ? lastArg : undefined;

Expand Down Expand Up @@ -207,7 +207,7 @@ RestAdapter.prototype.createHandler = function () {
});

// Set strict to be `false` so that anything `JSON.parse()` accepts will be parsed
debug("remoting options: %j", this.remotes.options);
debug('remoting options: %j', this.remotes.options);
var urlencodedOptions = this.remotes.options.urlencoded || {extended: true};
if (urlencodedOptions.extended === undefined) {
urlencodedOptions.extended = true;
Expand Down Expand Up @@ -305,29 +305,46 @@ RestAdapter.urlNotFoundHandler = function() {
RestAdapter.errorHandler = function(options) {
options = options || {};
return function restErrorHandler(err, req, res, next) {
if(typeof err === 'string') {
err = new Error(err);
err.status = err.statusCode = 500;
if (typeof options.handler === 'function') {
try {
options.handler(err, req, res, defaultHandler);
} catch(e) {
defaultHandler(e);
}
} else {
return defaultHandler();
}

res.statusCode = err.statusCode || err.status || 500;
function defaultHandler(handlerError) {
if(handlerError) {
// ensure errors that occurred during
// the handler are reported
err = handlerError;
}
if(typeof err === 'string') {
err = new Error(err);
err.status = err.statusCode = 500;
}

debug('Error in %s %s: %s', req.method, req.url, err.stack);
var data = {
name: err.name,
status: res.statusCode,
message: err.message || 'An unknown error occurred'
};
res.statusCode = err.statusCode || err.status || 500;

for (var prop in err) {
data[prop] = err[prop];
}
debug('Error in %s %s: %s', req.method, req.url, err.stack);
var data = {
name: err.name,
status: res.statusCode,
message: err.message || 'An unknown error occurred'
};

for (var prop in err) {
data[prop] = err[prop];
}

data.stack = err.stack;
if (process.env.NODE_ENV === 'production' || options.disableStackTrace) {
delete data.stack;
data.stack = err.stack;
if (process.env.NODE_ENV === 'production' || options.disableStackTrace) {
delete data.stack;
}
res.send({ error: data });
}
res.send({ error: data });
};
};

Expand Down
7 changes: 6 additions & 1 deletion lib/shared-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ SharedMethod.prototype.invoke = function (scope, args, fn) {
debug('- %s - invoke with', this.name, formattedArgs);

// invoke
return method.apply(scope, formattedArgs);
try {
return method.apply(scope, formattedArgs);
} catch (err) {
debug('error caught during the invocation of %s', this.name);
return fn(err);
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Remoting",
"REST"
],
"version": "2.5.0",
"version": "2.7.1",
"scripts": {
"test": "mocha"
},
Expand All @@ -18,12 +18,14 @@
"eventemitter2": "~0.4.14",
"cors": "~2.4.1",
"jayson": "~1.1.1",
"js2xmlparser": "~0.1.3",
"async": "~0.9.0",
"traverse": "~0.6.6",
"request": "~2.42.0",
"browser-request": "~0.3.2",
"qs": "~2.2.3",
"inflection": "~1.4.2"
"inflection": "~1.4.2",
"xml2js": "~0.4.4"
},
"devDependencies": {
"supertest": "~0.13.0",
Expand Down
4 changes: 1 addition & 3 deletions test/rest-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('RestAdapter', function() {
return new RestAdapter(remotes).getClasses();
}
});

describe('path normalization', function() {
it('fills `routes`', function() {
remotes.exports.testClass = factory.createSharedClass();
Expand Down Expand Up @@ -317,8 +317,6 @@ describe('RestAdapter', function() {
]);

});


});
});

Expand Down
Loading

0 comments on commit 334cb05

Please sign in to comment.