From 9a003d84d9ca05fb00b88e6aae025cfdca95dc8e Mon Sep 17 00:00:00 2001 From: David Luecke Date: Sun, 13 Mar 2016 12:47:43 -0700 Subject: [PATCH] Test and fix for request to serialize errors properly --- src/client/request.js | 6 +++++- test/client/request.test.js | 11 +++++++++-- test/client/server.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/client/request.js b/src/client/request.js index c64a096..10010c0 100644 --- a/src/client/request.js +++ b/src/client/request.js @@ -11,7 +11,11 @@ export default class Service extends Base { } if(!error && res.statusCode >= 400) { - return reject(new Error(data)); + if(typeof data === 'string') { + return reject(new Error(data)); + } + + return reject(Object.assign(new Error(data.message), data)); } resolve(data); diff --git a/test/client/request.test.js b/test/client/request.test.js index f78bc6d..86b8c74 100644 --- a/test/client/request.test.js +++ b/test/client/request.test.js @@ -33,11 +33,11 @@ describe('node-request REST connector', function() { query: {} })).then(done).catch(done); }); - + it('can initialize a client instance', done => { const init = rest(url).request(request); const todos = init.service('todos'); - + assert.ok(todos instanceof init.Service, 'Returned service is a client'); todos.find({}).then(todos => assert.deepEqual(todos, [ { @@ -47,4 +47,11 @@ describe('node-request REST connector', function() { } ])).then(() => done()).catch(done); }); + + it('converts errors properly', done => { + service.get(1, { query: { error: true } }).catch(e => { + assert.equal(e.message, 'Something went wrong'); + done(); + }).catch(done); + }); }); diff --git a/test/client/server.js b/test/client/server.js index fbc2835..d51adc0 100644 --- a/test/client/server.js +++ b/test/client/server.js @@ -3,6 +3,20 @@ import bodyParser from 'body-parser'; import memory from 'feathers-memory'; import rest from '../../src'; +Object.defineProperty(Error.prototype, 'toJSON', { + value: function () { + var alt = {}; + + Object.getOwnPropertyNames(this).forEach(function (key) { + alt[key] = this[key]; + }, this); + + return alt; + }, + configurable: true, + writable: true +}); + /*jshint unused: false*/ let errorHandler = function(error, req, res, next) { const code = !isNaN( parseInt(error.code, 10) ) ? parseInt(error.code, 10) : 500;