added tests for errors
Showing
2 changed files
with
52 additions
and
13 deletions
... | @@ -86,7 +86,7 @@ if (!phantom.casperLoaded) { | ... | @@ -86,7 +86,7 @@ if (!phantom.casperLoaded) { |
86 | // Embedded, up-to-date, validatable & controlable CoffeeScript | 86 | // Embedded, up-to-date, validatable & controlable CoffeeScript |
87 | phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js')); | 87 | phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js')); |
88 | 88 | ||
89 | // Adding built-in capabilities to phantom object | 89 | // Index of file sources, for error localization |
90 | phantom.sourceIds = {}; | 90 | phantom.sourceIds = {}; |
91 | 91 | ||
92 | // custom global CasperError | 92 | // custom global CasperError |
... | @@ -107,19 +107,16 @@ if (!phantom.casperLoaded) { | ... | @@ -107,19 +107,16 @@ if (!phantom.casperLoaded) { |
107 | 107 | ||
108 | // Stack formatting | 108 | // Stack formatting |
109 | window.CasperError.prototype.formatStack = function() { | 109 | window.CasperError.prototype.formatStack = function() { |
110 | var location; | 110 | var location = this.fileName || phantom.sourceIds[this.sourceId] || "unknown"; |
111 | if (this.fileName || this.sourceId) { | ||
112 | location = (this.fileName || phantom.sourceIds[this.sourceId]); | ||
113 | } else { | ||
114 | location = "unknown"; | ||
115 | } | ||
116 | location += this.line ? ':' + this.line : 0; | 111 | location += this.line ? ':' + this.line : 0; |
117 | return this.toString() + '\n ' + (this._from || "anonymous") + '() at ' + location; | 112 | return this.toString() + '\n ' + (this._from || "anonymous") + '() at ' + location; |
118 | }; | 113 | }; |
119 | 114 | ||
120 | // Adding stack traces to CasperError | 115 | /** |
121 | // Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ | 116 | * Adding pseudo stack traces to CasperError |
122 | // TODO: remove when phantomjs has js engine upgrade | 117 | * Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ |
118 | * TODO: remove when phantomjs has js engine upgrade | ||
119 | */ | ||
123 | if (!new CasperError().hasOwnProperty('stack')) { | 120 | if (!new CasperError().hasOwnProperty('stack')) { |
124 | Object.defineProperty(CasperError.prototype, 'stack', { | 121 | Object.defineProperty(CasperError.prototype, 'stack', { |
125 | set: function(string) { | 122 | set: function(string) { |
... | @@ -136,6 +133,12 @@ if (!phantom.casperLoaded) { | ... | @@ -136,6 +133,12 @@ if (!phantom.casperLoaded) { |
136 | }); | 133 | }); |
137 | } | 134 | } |
138 | 135 | ||
136 | /** | ||
137 | * Retrieves the javascript source code from a given .js or .coffee file. | ||
138 | * | ||
139 | * @param String file The path to the file | ||
140 | * @param Function|null onError An error callback (optional) | ||
141 | */ | ||
139 | phantom.getScriptCode = function(file, onError) { | 142 | phantom.getScriptCode = function(file, onError) { |
140 | var scriptCode = fs.read(file); | 143 | var scriptCode = fs.read(file); |
141 | if (/\.coffee$/i.test(file)) { | 144 | if (/\.coffee$/i.test(file)) { |
... | @@ -152,6 +155,17 @@ if (!phantom.casperLoaded) { | ... | @@ -152,6 +155,17 @@ if (!phantom.casperLoaded) { |
152 | return scriptCode; | 155 | return scriptCode; |
153 | }; | 156 | }; |
154 | 157 | ||
158 | /** | ||
159 | * Processes a given thrown Error; handles special cases and provides an | ||
160 | * optional callback argument. | ||
161 | * | ||
162 | * By default, the standard behavior on uncaught error is to print the | ||
163 | * error stack trace to the console and exit PhantomJS. | ||
164 | * | ||
165 | * @param Error error The Error instance | ||
166 | * @param String file A file path to associate to this error | ||
167 | * @param Function callback An optional callback | ||
168 | */ | ||
155 | phantom.processScriptError = function(error, file, callback) { | 169 | phantom.processScriptError = function(error, file, callback) { |
156 | if (!this.sourceIds.hasOwnProperty(error.sourceId)) { | 170 | if (!this.sourceIds.hasOwnProperty(error.sourceId)) { |
157 | this.sourceIds[error.sourceId] = file; | 171 | this.sourceIds[error.sourceId] = file; |
... | @@ -167,9 +181,12 @@ if (!phantom.casperLoaded) { | ... | @@ -167,9 +181,12 @@ if (!phantom.casperLoaded) { |
167 | } | 181 | } |
168 | }; | 182 | }; |
169 | 183 | ||
170 | // Patching require() | 184 | /** |
171 | // Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ | 185 | * Patching require() to allow loading of other modules than PhantomJS' |
172 | // TODO: remove when PhantomJS has full module support | 186 | * builtin ones. |
187 | * Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ | ||
188 | * TODO: remove when PhantomJS has full module support | ||
189 | */ | ||
173 | require = (function(require, requireDir) { | 190 | require = (function(require, requireDir) { |
174 | var phantomBuiltins = ['fs', 'webpage', 'webserver']; | 191 | var phantomBuiltins = ['fs', 'webpage', 'webserver']; |
175 | var phantomRequire = phantom.__orig__require = require; | 192 | var phantomRequire = phantom.__orig__require = require; | ... | ... |
tests/suites/error.js
0 → 100644
1 | (function(t) { | ||
2 | var error; | ||
3 | |||
4 | function foo() { | ||
5 | bar(); | ||
6 | } | ||
7 | |||
8 | function bar() { | ||
9 | throw new CasperError('bar'); | ||
10 | } | ||
11 | |||
12 | try { | ||
13 | foo(); | ||
14 | } catch (e) { | ||
15 | error = e; | ||
16 | } | ||
17 | |||
18 | t.assertType(error.stack, "string", "CasperError() has a stack string property set"); | ||
19 | t.assertMatch(error.stack, /^CasperError: bar\s/, "CasperError() has the expected stack value"); | ||
20 | |||
21 | t.done(); | ||
22 | })(casper.test); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment