[Pkg-javascript-commits] [dojo] 70/88: Catch handler exceptions in node provider (1.8 backport). !strict refs #16526

David Prévot taffit at moszumanska.debian.org
Thu Aug 21 17:39:40 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to annotated tag 1.8.5
in repository dojo.

commit 02f58e329677077074874d04109d5e75a8ef5cbf
Author: Bryan Forbes <bryan at reigndropsfall.net>
Date:   Sun Mar 3 21:27:46 2013 +0000

    Catch handler exceptions in node provider (1.8 backport). !strict refs #16526
    
    git-svn-id: http://svn.dojotoolkit.org/src/branches/1.8/dojo@30737 560b804f-0ae3-0310-86f3-f6aa0a117693
---
 request/node.js       | 10 +++++++---
 tests/request/node.js | 53 +++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/request/node.js b/request/node.js
index 4bdf2c1..8b9d5b6 100644
--- a/request/node.js
+++ b/request/node.js
@@ -92,15 +92,19 @@ define([
 					clearTimeout(timeout);
 				}
 				response.text = body.join('');
-				handlers(response);
-				def.resolve(response);
+				try{
+					handlers(response);
+					def.resolve(response);
+				}catch(error){
+					def.reject(error);
+				}
 			});
 		});
 
 		req.on('error', def.reject);
 
 		if(options.data){
-			if(typeof options.data === "string"){
+			if(typeof options.data === 'string'){
 				req.end(options.data);
 			}else{
 				options.data.pipe(req);
diff --git a/tests/request/node.js b/tests/request/node.js
index cf89aba..be45938 100644
--- a/tests/request/node.js
+++ b/tests/request/node.js
@@ -3,10 +3,28 @@ require([
 	'doh/main',
 	'dojo/request',
 	'dojo/node!http',
+	'dojo/node!url',
 	'dojo/Deferred'
-], function(require, doh, request, http, Deferred){
+], function(require, doh, request, http, url, Deferred){
+	var serverPort = 8142,
+		serverUrl = 'http://localhost:8124';
+
+	var responseDataMap = {
+		'fooBar': '{ "foo": "bar" }',
+		'invalidJson': '<not>JSON</not>'
+	};
+	function getRequestUrl(dataKey){
+		return serverUrl + '?dataKey=' + dataKey;
+	}
+	function getResponseData(request){
+		var parseQueryString = true;
+		var urlInfo = url.parse(request.url, parseQueryString);
+		return responseDataMap[urlInfo.query.dataKey];
+	}
+
 	var server = http.createServer(function(request, response){
-		var body = '{ "foo": "bar" }';
+		var body = getResponseData(request);
+
 		response.writeHead(200, {
 			'Content-Length': body.length,
 			'Content-Type': 'application/json'
@@ -15,14 +33,16 @@ require([
 		response.end();
 	});
 
+	function setUp(){ /* Do nothing */ }
+	function tearDown(){ server.close(); }
 	server.on('listening', function(){
-		doh.register("tests.request.node", [
+		var tests = [
 			{
-				name: "test",
+				name: 'test',
 				runTest: function(t){
 					var d = new doh.Deferred();
 
-					request.get('http://localhost:8124', {
+					request.get(getRequestUrl('fooBar'), {
 						handleAs: 'json'
 					}).then(d.getTestCallback(function(data){
 						t.is({ foo: 'bar' }, data);
@@ -31,15 +51,28 @@ require([
 					});
 
 					return d;
-				},
-				tearDown: function(){
-					server.close();
+				}
+			},
+			{
+				name: 'test-handler-exception',
+				runTest: function(t){
+					var d = new doh.Deferred();
+
+					request.get(getRequestUrl('invalidJson'), {
+						handleAs: 'json'
+					}).then(function(){
+						d.errback(new Error('Expected a handler exception.'));
+					}, d.getTestCallback(function(err){
+						doh.assertTrue(err instanceof SyntaxError);
+					}));
+
+					return d;
 				}
 			}
-		]);
+		];
 
+		doh.register('tests.request.node', tests, setUp, tearDown);
 		doh.run();
 	});
-
 	server.listen(8124);
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/dojo.git



More information about the Pkg-javascript-commits mailing list