[d565449] | 1 | (function(root, factory) {
|
---|
| 2 | 'use strict';
|
---|
| 3 | // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, Rhino, and browsers.
|
---|
| 4 |
|
---|
| 5 | /* istanbul ignore next */
|
---|
| 6 | if (typeof define === 'function' && define.amd) {
|
---|
| 7 | define('error-stack-parser', ['stackframe'], factory);
|
---|
| 8 | } else if (typeof exports === 'object') {
|
---|
| 9 | module.exports = factory(require('stackframe'));
|
---|
| 10 | } else {
|
---|
| 11 | root.ErrorStackParser = factory(root.StackFrame);
|
---|
| 12 | }
|
---|
| 13 | }(this, function ErrorStackParser(StackFrame) {
|
---|
| 14 | 'use strict';
|
---|
| 15 |
|
---|
| 16 | var FIREFOX_SAFARI_STACK_REGEXP = /(^|@)\S+:\d+/;
|
---|
| 17 | var CHROME_IE_STACK_REGEXP = /^\s*at .*(\S+:\d+|\(native\))/m;
|
---|
| 18 | var SAFARI_NATIVE_CODE_REGEXP = /^(eval@)?(\[native code])?$/;
|
---|
| 19 |
|
---|
| 20 | return {
|
---|
| 21 | /**
|
---|
| 22 | * Given an Error object, extract the most information from it.
|
---|
| 23 | *
|
---|
| 24 | * @param {Error} error object
|
---|
| 25 | * @return {Array} of StackFrames
|
---|
| 26 | */
|
---|
| 27 | parse: function ErrorStackParser$$parse(error) {
|
---|
| 28 | if (typeof error.stacktrace !== 'undefined' || typeof error['opera#sourceloc'] !== 'undefined') {
|
---|
| 29 | return this.parseOpera(error);
|
---|
| 30 | } else if (error.stack && error.stack.match(CHROME_IE_STACK_REGEXP)) {
|
---|
| 31 | return this.parseV8OrIE(error);
|
---|
| 32 | } else if (error.stack) {
|
---|
| 33 | return this.parseFFOrSafari(error);
|
---|
| 34 | } else {
|
---|
| 35 | throw new Error('Cannot parse given Error object');
|
---|
| 36 | }
|
---|
| 37 | },
|
---|
| 38 |
|
---|
| 39 | // Separate line and column numbers from a string of the form: (URI:Line:Column)
|
---|
| 40 | extractLocation: function ErrorStackParser$$extractLocation(urlLike) {
|
---|
| 41 | // Fail-fast but return locations like "(native)"
|
---|
| 42 | if (urlLike.indexOf(':') === -1) {
|
---|
| 43 | return [urlLike];
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | var regExp = /(.+?)(?::(\d+))?(?::(\d+))?$/;
|
---|
| 47 | var parts = regExp.exec(urlLike.replace(/[()]/g, ''));
|
---|
| 48 | return [parts[1], parts[2] || undefined, parts[3] || undefined];
|
---|
| 49 | },
|
---|
| 50 |
|
---|
| 51 | parseV8OrIE: function ErrorStackParser$$parseV8OrIE(error) {
|
---|
| 52 | var filtered = error.stack.split('\n').filter(function(line) {
|
---|
| 53 | return !!line.match(CHROME_IE_STACK_REGEXP);
|
---|
| 54 | }, this);
|
---|
| 55 |
|
---|
| 56 | return filtered.map(function(line) {
|
---|
| 57 | if (line.indexOf('(eval ') > -1) {
|
---|
| 58 | // Throw away eval information until we implement stacktrace.js/stackframe#8
|
---|
| 59 | line = line.replace(/eval code/g, 'eval').replace(/(\(eval at [^()]*)|(,.*$)/g, '');
|
---|
| 60 | }
|
---|
| 61 | var sanitizedLine = line.replace(/^\s+/, '').replace(/\(eval code/g, '(').replace(/^.*?\s+/, '');
|
---|
| 62 |
|
---|
| 63 | // capture and preseve the parenthesized location "(/foo/my bar.js:12:87)" in
|
---|
| 64 | // case it has spaces in it, as the string is split on \s+ later on
|
---|
| 65 | var location = sanitizedLine.match(/ (\(.+\)$)/);
|
---|
| 66 |
|
---|
| 67 | // remove the parenthesized location from the line, if it was matched
|
---|
| 68 | sanitizedLine = location ? sanitizedLine.replace(location[0], '') : sanitizedLine;
|
---|
| 69 |
|
---|
| 70 | // if a location was matched, pass it to extractLocation() otherwise pass all sanitizedLine
|
---|
| 71 | // because this line doesn't have function name
|
---|
| 72 | var locationParts = this.extractLocation(location ? location[1] : sanitizedLine);
|
---|
| 73 | var functionName = location && sanitizedLine || undefined;
|
---|
| 74 | var fileName = ['eval', '<anonymous>'].indexOf(locationParts[0]) > -1 ? undefined : locationParts[0];
|
---|
| 75 |
|
---|
| 76 | return new StackFrame({
|
---|
| 77 | functionName: functionName,
|
---|
| 78 | fileName: fileName,
|
---|
| 79 | lineNumber: locationParts[1],
|
---|
| 80 | columnNumber: locationParts[2],
|
---|
| 81 | source: line
|
---|
| 82 | });
|
---|
| 83 | }, this);
|
---|
| 84 | },
|
---|
| 85 |
|
---|
| 86 | parseFFOrSafari: function ErrorStackParser$$parseFFOrSafari(error) {
|
---|
| 87 | var filtered = error.stack.split('\n').filter(function(line) {
|
---|
| 88 | return !line.match(SAFARI_NATIVE_CODE_REGEXP);
|
---|
| 89 | }, this);
|
---|
| 90 |
|
---|
| 91 | return filtered.map(function(line) {
|
---|
| 92 | // Throw away eval information until we implement stacktrace.js/stackframe#8
|
---|
| 93 | if (line.indexOf(' > eval') > -1) {
|
---|
| 94 | line = line.replace(/ line (\d+)(?: > eval line \d+)* > eval:\d+:\d+/g, ':$1');
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if (line.indexOf('@') === -1 && line.indexOf(':') === -1) {
|
---|
| 98 | // Safari eval frames only have function names and nothing else
|
---|
| 99 | return new StackFrame({
|
---|
| 100 | functionName: line
|
---|
| 101 | });
|
---|
| 102 | } else {
|
---|
| 103 | var functionNameRegex = /((.*".+"[^@]*)?[^@]*)(?:@)/;
|
---|
| 104 | var matches = line.match(functionNameRegex);
|
---|
| 105 | var functionName = matches && matches[1] ? matches[1] : undefined;
|
---|
| 106 | var locationParts = this.extractLocation(line.replace(functionNameRegex, ''));
|
---|
| 107 |
|
---|
| 108 | return new StackFrame({
|
---|
| 109 | functionName: functionName,
|
---|
| 110 | fileName: locationParts[0],
|
---|
| 111 | lineNumber: locationParts[1],
|
---|
| 112 | columnNumber: locationParts[2],
|
---|
| 113 | source: line
|
---|
| 114 | });
|
---|
| 115 | }
|
---|
| 116 | }, this);
|
---|
| 117 | },
|
---|
| 118 |
|
---|
| 119 | parseOpera: function ErrorStackParser$$parseOpera(e) {
|
---|
| 120 | if (!e.stacktrace || (e.message.indexOf('\n') > -1 &&
|
---|
| 121 | e.message.split('\n').length > e.stacktrace.split('\n').length)) {
|
---|
| 122 | return this.parseOpera9(e);
|
---|
| 123 | } else if (!e.stack) {
|
---|
| 124 | return this.parseOpera10(e);
|
---|
| 125 | } else {
|
---|
| 126 | return this.parseOpera11(e);
|
---|
| 127 | }
|
---|
| 128 | },
|
---|
| 129 |
|
---|
| 130 | parseOpera9: function ErrorStackParser$$parseOpera9(e) {
|
---|
| 131 | var lineRE = /Line (\d+).*script (?:in )?(\S+)/i;
|
---|
| 132 | var lines = e.message.split('\n');
|
---|
| 133 | var result = [];
|
---|
| 134 |
|
---|
| 135 | for (var i = 2, len = lines.length; i < len; i += 2) {
|
---|
| 136 | var match = lineRE.exec(lines[i]);
|
---|
| 137 | if (match) {
|
---|
| 138 | result.push(new StackFrame({
|
---|
| 139 | fileName: match[2],
|
---|
| 140 | lineNumber: match[1],
|
---|
| 141 | source: lines[i]
|
---|
| 142 | }));
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return result;
|
---|
| 147 | },
|
---|
| 148 |
|
---|
| 149 | parseOpera10: function ErrorStackParser$$parseOpera10(e) {
|
---|
| 150 | var lineRE = /Line (\d+).*script (?:in )?(\S+)(?:: In function (\S+))?$/i;
|
---|
| 151 | var lines = e.stacktrace.split('\n');
|
---|
| 152 | var result = [];
|
---|
| 153 |
|
---|
| 154 | for (var i = 0, len = lines.length; i < len; i += 2) {
|
---|
| 155 | var match = lineRE.exec(lines[i]);
|
---|
| 156 | if (match) {
|
---|
| 157 | result.push(
|
---|
| 158 | new StackFrame({
|
---|
| 159 | functionName: match[3] || undefined,
|
---|
| 160 | fileName: match[2],
|
---|
| 161 | lineNumber: match[1],
|
---|
| 162 | source: lines[i]
|
---|
| 163 | })
|
---|
| 164 | );
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | return result;
|
---|
| 169 | },
|
---|
| 170 |
|
---|
| 171 | // Opera 10.65+ Error.stack very similar to FF/Safari
|
---|
| 172 | parseOpera11: function ErrorStackParser$$parseOpera11(error) {
|
---|
| 173 | var filtered = error.stack.split('\n').filter(function(line) {
|
---|
| 174 | return !!line.match(FIREFOX_SAFARI_STACK_REGEXP) && !line.match(/^Error created at/);
|
---|
| 175 | }, this);
|
---|
| 176 |
|
---|
| 177 | return filtered.map(function(line) {
|
---|
| 178 | var tokens = line.split('@');
|
---|
| 179 | var locationParts = this.extractLocation(tokens.pop());
|
---|
| 180 | var functionCall = (tokens.shift() || '');
|
---|
| 181 | var functionName = functionCall
|
---|
| 182 | .replace(/<anonymous function(: (\w+))?>/, '$2')
|
---|
| 183 | .replace(/\([^)]*\)/g, '') || undefined;
|
---|
| 184 | var argsRaw;
|
---|
| 185 | if (functionCall.match(/\(([^)]*)\)/)) {
|
---|
| 186 | argsRaw = functionCall.replace(/^[^(]+\(([^)]*)\)$/, '$1');
|
---|
| 187 | }
|
---|
| 188 | var args = (argsRaw === undefined || argsRaw === '[arguments not available]') ?
|
---|
| 189 | undefined : argsRaw.split(',');
|
---|
| 190 |
|
---|
| 191 | return new StackFrame({
|
---|
| 192 | functionName: functionName,
|
---|
| 193 | args: args,
|
---|
| 194 | fileName: locationParts[0],
|
---|
| 195 | lineNumber: locationParts[1],
|
---|
| 196 | columnNumber: locationParts[2],
|
---|
| 197 | source: line
|
---|
| 198 | });
|
---|
| 199 | }, this);
|
---|
| 200 | }
|
---|
| 201 | };
|
---|
| 202 | }));
|
---|