Skip to content

Commit

Permalink
Handle uncaughtExceptions better.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kami committed Mar 27, 2011
1 parent 55e61fb commit 53535b5
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
39 changes: 39 additions & 0 deletions example/some-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Cloudkick, Inc ('Cloudkick') under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* Cloudkick licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var throwException = function() {
throw new Error('failure!');
};

var throwNestedFunctions = function() {
var function1 = function(callback) {
var function2 = function(callback) {
var function3 = function(callback) {
throw new Error('woho, failure!');
};

function3(callback);
};

function2(callback);
};

function1(function() {});
};

exports.throwException = throwException;
exports.throwNestedFunctions = throwNestedFunctions;
39 changes: 39 additions & 0 deletions example/test-uncaught.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Cloudkick, Inc ('Cloudkick') under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* Cloudkick licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require.paths.unshift(__dirname);
var someFile = require('some-file');

exports['test_uncaught_exception'] = function() {
throw new Error('Testing uncaught exception');
};

exports['test_uncaught_exception_2'] = function() {
someFile.throwException();
};

exports['test_unknown_method'] = function() {
someFile.unknownMethod();
};

exports['test_uncaught_nested_functions'] = function() {
someFile.throwNestedFunctions();
};

exports['test1_require_throws'] = function() {
require('timeout-throws.js');
};
13 changes: 13 additions & 0 deletions example/timeout-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function initSecondTimeout(tag) {
setTimeout(function secondTimeout() {
throw new Error(tag);
}, 100);
}

function onload() {
setTimeout(function firstTimeout() {
initSecondTimeout("timeout");
}, 100);
}

onload();
4 changes: 4 additions & 0 deletions lib/run_test_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ process.nextTick(function() {
}
});

process.on('uncaughtException', function(err) {
failed['uncaught_exception'] = err;
});

process.on('exit', function() {
common.printChildResults(succeeded, failed);
process.reallyExit(failed.length);
Expand Down
10 changes: 9 additions & 1 deletion test/simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ if [ $? -ne 1 ]; then
fi

# Test relative path

"${CWD}/bin/whiskey" --tests "../example/test-success.js"

if [ $? -ne 0 ]; then
Expand All @@ -63,6 +62,7 @@ fi
# Test init file
FOLDER_EXISTS=0
rm -rf ${CWD}/example/test-123456

"${CWD}/bin/whiskey" --init-file "${CWD}/example/init.js" --tests "${CWD}/example/test-success.js"

if [ -d ${CWD}/example/test-123456 ]; then
Expand All @@ -76,4 +76,12 @@ if [ $? -ne 0 ] || [ ${FOLDER_EXISTS} -ne 1 ]; then
exit 1
fi

# Uncaught exceptions
"${CWD}/bin/whiskey" --tests "${CWD}/example/test-uncaught.js"

if [ $? -ne 5 ]; then
echo "Test should fail but passed."
exit 1
fi

exit 0

0 comments on commit 53535b5

Please sign in to comment.