Skip to content

Commit 7a94205

Browse files
committed
Catch handler exceptions in node provider. !strict refs #16526
git-svn-id: http://svn.dojotoolkit.org/src/dojo/trunk@30736 560b804f-0ae3-0310-86f3-f6aa0a117693
1 parent ffd100d commit 7a94205

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

request/node.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,19 @@ define([
9292
clearTimeout(timeout);
9393
}
9494
response.text = body.join('');
95-
handlers(response);
96-
def.resolve(response);
95+
try{
96+
handlers(response);
97+
def.resolve(response);
98+
}catch(error){
99+
def.reject(error);
100+
}
97101
});
98102
});
99103

100104
req.on('error', def.reject);
101105

102106
if(options.data){
103-
if(typeof options.data === "string"){
107+
if(typeof options.data === 'string'){
104108
req.end(options.data);
105109
}else{
106110
options.data.pipe(req);

tests/request/node.js

+43-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,28 @@ require([
33
'doh/main',
44
'dojo/request',
55
'dojo/node!http',
6+
'dojo/node!url',
67
'dojo/Deferred'
7-
], function(require, doh, request, http, Deferred){
8+
], function(require, doh, request, http, url, Deferred){
9+
var serverPort = 8142,
10+
serverUrl = 'http://localhost:8124';
11+
12+
var responseDataMap = {
13+
'fooBar': '{ "foo": "bar" }',
14+
'invalidJson': '<not>JSON</not>'
15+
};
16+
function getRequestUrl(dataKey){
17+
return serverUrl + '?dataKey=' + dataKey;
18+
}
19+
function getResponseData(request){
20+
var parseQueryString = true;
21+
var urlInfo = url.parse(request.url, parseQueryString);
22+
return responseDataMap[urlInfo.query.dataKey];
23+
}
24+
825
var server = http.createServer(function(request, response){
9-
var body = '{ "foo": "bar" }';
26+
var body = getResponseData(request);
27+
1028
response.writeHead(200, {
1129
'Content-Length': body.length,
1230
'Content-Type': 'application/json'
@@ -15,14 +33,16 @@ require([
1533
response.end();
1634
});
1735

36+
function setUp(){ /* Do nothing */ }
37+
function tearDown(){ server.close(); }
1838
server.on('listening', function(){
19-
doh.register("tests.request.node", [
39+
var tests = [
2040
{
21-
name: "test",
41+
name: 'test',
2242
runTest: function(t){
2343
var d = new doh.Deferred();
2444

25-
request.get('http://localhost:8124', {
45+
request.get(getRequestUrl('fooBar'), {
2646
handleAs: 'json'
2747
}).then(d.getTestCallback(function(data){
2848
t.is({ foo: 'bar' }, data);
@@ -31,15 +51,28 @@ require([
3151
});
3252

3353
return d;
34-
},
35-
tearDown: function(){
36-
server.close();
54+
}
55+
},
56+
{
57+
name: 'test-handler-exception',
58+
runTest: function(t){
59+
var d = new doh.Deferred();
60+
61+
request.get(getRequestUrl('invalidJson'), {
62+
handleAs: 'json'
63+
}).then(function(){
64+
d.errback(new Error('Expected a handler exception.'));
65+
}, d.getTestCallback(function(err){
66+
doh.assertTrue(err instanceof SyntaxError);
67+
}));
68+
69+
return d;
3770
}
3871
}
39-
]);
72+
];
4073

74+
doh.register('tests.request.node', tests, setUp, tearDown);
4175
doh.run();
4276
});
43-
4477
server.listen(8124);
4578
});

0 commit comments

Comments
 (0)