Skip to content

Commit 5655245

Browse files
committed
fixed error handling
1 parent 0513d14 commit 5655245

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ function jsonscriptProxy(options) {
6666
return new (options.Promise || Promise)(function (resolve, reject) {
6767
request[args.method](opts, function (err, resp) {
6868
if (err) return reject(err);
69-
resolve(processResponse(resp, args));
69+
try { resolve(processResponse(resp, args)); }
70+
catch(e) { reject(e); }
7071
});
7172
});
7273
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonscript-proxy",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Proxy server for scripted processing of other services using JSONScript",
55
"main": "index.js",
66
"scripts": {

spec/proxy_handler.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe('jsonscript proxy handler', function() {
205205
});
206206

207207
describe('error handling', function() {
208-
it('should return error if script is invalid', function() {
208+
it('should return error if script is invalid', function (done) {
209209
send({
210210
script: {
211211
$exec: 'service1',
@@ -216,7 +216,7 @@ describe('jsonscript proxy handler', function() {
216216
$wrongProperty: true
217217
}
218218
}, function (err, resp) {
219-
assert(!!err);
219+
assert.equal(err.message, 'expected 200 "OK", got 400 "Bad Request"');
220220
assert.equal(resp.statusCode, 400);
221221
assert.equal(resp.body.error, 'script is invalid');
222222
done();
@@ -299,6 +299,23 @@ describe('jsonscript proxy handler', function() {
299299
done();
300300
});
301301
});
302+
303+
it('should return error if statusCode is >= 300', function (done) {
304+
send({
305+
script: {
306+
'$$service1.get': { path: '/object/1/error' }
307+
}
308+
}, function (err, resp) {
309+
assert.equal(err.message, 'expected 200 "OK", got 500 "Internal Server Error"');
310+
assert.deepEqual(JSON.parse(resp.body.error), {
311+
name: 'object',
312+
id: 1,
313+
service: 'service1',
314+
info: 'resource object id 1'
315+
});
316+
done();
317+
});
318+
});
302319
});
303320
});
304321

spec/service.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ module.exports = function startService(serviceName, port) {
1111
app.use(bodyParser.json());
1212

1313
app.get('/api/:name/:id', function (req, res) {
14-
var id = req.params.id;
15-
send(res, req.params.name, id);
14+
send(res, req.params.name, req.params.id);
1615
});
1716

1817
app.post('/api/:name', function (req, res) {
1918
var id = Date.now();
2019
send(res, req.params.name, id, req.body);
2120
});
2221

22+
app.get('/api/:name/:id/error', function (req, res) {
23+
send(res.status(500), req.params.name, req.params.id);
24+
});
25+
2326

2427
function send(res, name, id, data) {
2528
res.send({

0 commit comments

Comments
 (0)