Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

Commit

Permalink
Test and fix for request to serialize errors properly
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Mar 13, 2016
1 parent fd51d8d commit 9a003d8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/client/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions test/client/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
{
Expand All @@ -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);
});
});
14 changes: 14 additions & 0 deletions test/client/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9a003d8

Please sign in to comment.