better error handling
Showing
6 changed files
with
29 additions
and
7 deletions
... | @@ -69,12 +69,16 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); | ... | @@ -69,12 +69,16 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); |
69 | } | 69 | } |
70 | 70 | ||
71 | function __die(message) { | 71 | function __die(message) { |
72 | console.error(message); | 72 | if (message) { |
73 | console.error(message); | ||
74 | } | ||
73 | phantom.exit(1); | 75 | phantom.exit(1); |
74 | } | 76 | } |
75 | 77 | ||
76 | function __terminate(message) { | 78 | function __terminate(message) { |
77 | console.log(message); | 79 | if (message) { |
80 | console.log(message); | ||
81 | } | ||
78 | phantom.exit(); | 82 | phantom.exit(); |
79 | } | 83 | } |
80 | 84 | ||
... | @@ -90,13 +94,17 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); | ... | @@ -90,13 +94,17 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); |
90 | } | 94 | } |
91 | })(phantom.version); | 95 | })(phantom.version); |
92 | 96 | ||
93 | // Hooks in default phantomjs error handler to print a hint when a possible | 97 | // Hooks in default phantomjs error handler |
94 | // casperjs command misuse is detected. | ||
95 | phantom.onError = function onPhantomError(msg, trace) { | 98 | phantom.onError = function onPhantomError(msg, trace) { |
96 | phantom.defaultErrorHandler.apply(phantom, arguments); | 99 | phantom.defaultErrorHandler.apply(phantom, arguments); |
100 | // print a hint when a possible casperjs command misuse is detected | ||
97 | if (msg.indexOf("ReferenceError: Can't find variable: casper") === 0) { | 101 | if (msg.indexOf("ReferenceError: Can't find variable: casper") === 0) { |
98 | console.error('Hint: you may want to use the `casperjs test` command.'); | 102 | console.error('Hint: you may want to use the `casperjs test` command.'); |
99 | } | 103 | } |
104 | // exits on syntax error | ||
105 | if (msg.indexOf('SyntaxError: Parse error') === 0) { | ||
106 | __die(); | ||
107 | } | ||
100 | }; | 108 | }; |
101 | 109 | ||
102 | // Patching fs | 110 | // Patching fs | ... | ... |
... | @@ -360,7 +360,6 @@ Casper.prototype.checkStep = function checkStep(self, onComplete) { | ... | @@ -360,7 +360,6 @@ Casper.prototype.checkStep = function checkStep(self, onComplete) { |
360 | } | 360 | } |
361 | } catch (error) { | 361 | } catch (error) { |
362 | self.emit('complete.error', error); | 362 | self.emit('complete.error', error); |
363 | throw error; | ||
364 | } | 363 | } |
365 | }; | 364 | }; |
366 | 365 | ||
... | @@ -1372,7 +1371,6 @@ Casper.prototype.runStep = function runStep(step) { | ... | @@ -1372,7 +1371,6 @@ Casper.prototype.runStep = function runStep(step) { |
1372 | } | 1371 | } |
1373 | } catch (err) { | 1372 | } catch (err) { |
1374 | this.emit('step.error', err); | 1373 | this.emit('step.error', err); |
1375 | throw err; | ||
1376 | } | 1374 | } |
1377 | if (!skipLog) { | 1375 | if (!skipLog) { |
1378 | this.emit('step.complete', stepResult); | 1376 | this.emit('step.complete', stepResult); | ... | ... |
... | @@ -259,7 +259,7 @@ Tester.prototype.assertTrue = function assert(subject, message, context) { | ... | @@ -259,7 +259,7 @@ Tester.prototype.assertTrue = function assert(subject, message, context) { |
259 | } | 259 | } |
260 | }, context || {}); | 260 | }, context || {}); |
261 | if (!result.success && result.doThrow) { | 261 | if (!result.success && result.doThrow) { |
262 | throw new AssertionError(message, result); | 262 | throw new AssertionError(message || result.standard, result); |
263 | } | 263 | } |
264 | return this.processAssertionResult(result); | 264 | return this.processAssertionResult(result); |
265 | }; | 265 | }; | ... | ... |
tests/clitests/error/syntax.js
0 → 100644
1 | qsfjdhf!èrè"èqwnfkmsghfkswjdgfjksn |
... | @@ -81,6 +81,20 @@ class CasperExecTest(unittest.TestCase): | ... | @@ -81,6 +81,20 @@ class CasperExecTest(unittest.TestCase): |
81 | self.assertCommandOutputEquals(script_path, 'it works') | 81 | self.assertCommandOutputEquals(script_path, 'it works') |
82 | 82 | ||
83 | @timeout(20) | 83 | @timeout(20) |
84 | def test_syntax_error(self): | ||
85 | script_path = os.path.join(TEST_ROOT, 'error', 'syntax.js') | ||
86 | self.assertCommandOutputContains(script_path, [ | ||
87 | 'SyntaxError: Parse error', | ||
88 | ], failing=True) | ||
89 | |||
90 | @timeout(20) | ||
91 | def test_syntax_error_in_test(self): | ||
92 | script_path = os.path.join(TEST_ROOT, 'error', 'syntax.js') | ||
93 | self.assertCommandOutputContains('test %s' % script_path, [ | ||
94 | 'SyntaxError: Parse error', | ||
95 | ], failing=True) | ||
96 | |||
97 | @timeout(20) | ||
84 | def test_simple_test_script(self): | 98 | def test_simple_test_script(self): |
85 | script_path = os.path.join(TEST_ROOT, 'tester', 'mytest.js') | 99 | script_path = os.path.join(TEST_ROOT, 'tester', 'mytest.js') |
86 | self.assertCommandOutputContains('test ' + script_path, [ | 100 | self.assertCommandOutputContains('test ' + script_path, [ | ... | ... |
-
Please register or sign in to post a comment