Skip to content

Commit

Permalink
Re-throw an error for rejecting Promise (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
koichik authored and Vijar committed May 10, 2016
1 parent b8c0a21 commit 75c99ee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
4 changes: 3 additions & 1 deletion libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ Request.prototype.end = function (callback) {
debug('Executing request %s.%s with params %o and body %o', self.resource, self.operation, self._params, self._body);
setImmediate(executeRequest, self, resolve, reject);
});
promise = promise.then(captureMeta, captureMeta);
promise = promise.then(captureMeta, function (err) {
throw captureMeta(err);
});
return promise;
}
};
Expand Down
71 changes: 49 additions & 22 deletions tests/util/testCrud.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,55 @@ module.exports = function testCrud (params, body, config, callback, resolve, rej
});
});
});
it('should have serviceMeta data on error', function (done) {
var fetcher = this.fetcher;
fetcher._serviceMeta.length = 0; // reset serviceMeta to empty array
fetcher
.read(mockErrorService.name)
.params(_.merge({}, params, {
meta: {
headers: {
'x-foo': 'foo'
}
}
}))
.clientConfig(config)
.end(function (err) {
if (err) {
var serviceMeta = fetcher.getServiceMeta();
expect(serviceMeta).to.have.length(1);
expect(serviceMeta[0]).to.include.keys('headers');
expect(serviceMeta[0].headers).to.include.keys('x-foo');
expect(serviceMeta[0].headers['x-foo']).to.equal('foo');
done();
}
describe('should have serviceMeta data on error', function() {
it('with callbacks', function (done) {
var fetcher = this.fetcher;
fetcher._serviceMeta.length = 0; // reset serviceMeta to empty array
fetcher
.read(mockErrorService.name)
.params(_.merge({}, params, {
meta: {
headers: {
'x-foo': 'foo'
}
}
}))
.clientConfig(config)
.end(function (err) {
if (err) {
var serviceMeta = fetcher.getServiceMeta();
expect(serviceMeta).to.have.length(1);
expect(serviceMeta[0]).to.include.keys('headers');
expect(serviceMeta[0].headers).to.include.keys('x-foo');
expect(serviceMeta[0].headers['x-foo']).to.equal('foo');
done();
}
});
});
it('with Promises', function (done) {
var fetcher = this.fetcher;
fetcher._serviceMeta.length = 0; // reset serviceMeta to empty array
fetcher
.read(mockErrorService.name)
.params(_.merge({}, params, {
meta: {
headers: {
'x-foo': 'foo'
}
}
}))
.clientConfig(config)
.end()
.catch(function (err) {
if (err) {
var serviceMeta = fetcher.getServiceMeta();
expect(serviceMeta).to.have.length(1);
expect(serviceMeta[0]).to.include.keys('headers');
expect(serviceMeta[0].headers).to.include.keys('x-foo');
expect(serviceMeta[0].headers['x-foo']).to.equal('foo');
done();
}
});
});
});
});
Expand Down

0 comments on commit 75c99ee

Please sign in to comment.