Skip to content

Commit bde4031

Browse files
author
Eric Wendelin
committed
No construction necessary - ErrorStackParser.parse(error).
1 parent e1c8c59 commit bde4031

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error-stack-parser.js
1+
error-stack-parser.js - Extract meaning from JS Errors
22
===============
33
[![Build Status](https://travis-ci.org/stacktracejs/error-stack-parser.svg?branch=master)](https://travis-ci.org/stacktracejs/error-stack-parser) [![Coverage Status](https://img.shields.io/coveralls/stacktracejs/error-stack-parser.svg)](https://coveralls.io/r/stacktracejs/error-stack-parser) [![Code Climate](https://codeclimate.com/github/stacktracejs/error-stack-parser/badges/gpa.svg)](https://codeclimate.com/github/stacktracejs/error-stack-parser)
44

5-
Extract meaning from JS Errors
5+
## Usage
6+
```
7+
ErrorStackParser.parse(new Error("sample"));
8+
9+
=> [StackFrame('funName1', [], 'path/to/file.js', 35, 79), StackFrame(..)]
10+
```
611

712
## Installation
813
```

error-stack-parser.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
} else {
1010
root.ErrorStackParser = factory(root.StackFrame);
1111
}
12-
}(this, function () {
12+
}(this, function ErrorStackParser(StackFrame) {
1313
'use strict';
1414

1515
// ES5 Polyfills
@@ -97,33 +97,33 @@
9797
};
9898
}
9999

100-
return function ErrorStackParser() {
101-
this.firefoxSafariStackEntryRegExp = /\S+\:\d+/;
102-
this.chromeIEStackEntryRegExp = /\s+at /;
100+
var FIREFOX_SAFARI_STACK_REGEXP = /\S+\:\d+/;
101+
var CHROME_IE_STACK_REGEXP = /\s+at /;
103102

103+
return {
104104
/**
105105
* Given an Error object, extract the most information from it.
106106
* @param error {Error}
107107
* @return Array[StackFrame]
108108
*/
109-
this.parse = function parse(error) {
109+
parse: function parse(error) {
110110
if (typeof error.stacktrace !== 'undefined' || typeof error['opera#sourceloc'] !== 'undefined') {
111111
return this.parseOpera(error);
112-
} else if (error.stack.match(this.chromeIEStackEntryRegExp)) {
112+
} else if (error.stack.match(CHROME_IE_STACK_REGEXP)) {
113113
return this.parseV8OrIE(error);
114-
} else if (error.stack.match(this.firefoxSafariStackEntryRegExp)) {
114+
} else if (error.stack.match(FIREFOX_SAFARI_STACK_REGEXP)) {
115115
return this.parseFFOrSafari(error);
116116
} else {
117117
throw new Error('Cannot parse given Error object');
118118
}
119-
};
119+
},
120120

121121
/**
122122
* Separate line and column numbers from a URL-like string.
123123
* @param urlLike String
124124
* @return Array[String]
125125
*/
126-
this.extractLocation = function extractLocation(urlLike) {
126+
extractLocation: function extractLocation(urlLike) {
127127
var locationParts = urlLike.split(':');
128128
var lastNumber = locationParts.pop();
129129
var possibleNumber = locationParts[locationParts.length - 1];
@@ -133,29 +133,29 @@
133133
} else {
134134
return [locationParts.join(':'), lastNumber, undefined];
135135
}
136-
};
136+
},
137137

138-
this.parseV8OrIE = function parseV8OrIE(error) {
138+
parseV8OrIE: function parseV8OrIE(error) {
139139
return error.stack.split('\n').slice(1).map(function (line) {
140140
var tokens = line.replace(/^\s+/, '').split(/\s+/).slice(1);
141141
var locationParts = this.extractLocation(tokens.pop().replace(/[\(\)\s]/g, ''));
142142
var functionName = (!tokens[0] || tokens[0] === 'Anonymous') ? undefined : tokens[0];
143143
return new StackFrame(functionName, undefined, locationParts[0], locationParts[1], locationParts[2]);
144144
}.bind(this));
145-
};
145+
},
146146

147-
this.parseFFOrSafari = function parseFFOrSafari(error) {
147+
parseFFOrSafari: function parseFFOrSafari(error) {
148148
return error.stack.split('\n').filter(function (line) {
149-
return !!line.match(this.firefoxSafariStackEntryRegExp);
149+
return !!line.match(FIREFOX_SAFARI_STACK_REGEXP);
150150
}.bind(this)).map(function (line) {
151151
var tokens = line.split('@');
152152
var locationParts = this.extractLocation(tokens.pop());
153153
var functionName = tokens.shift() || undefined;
154154
return new StackFrame(functionName, undefined, locationParts[0], locationParts[1], locationParts[2]);
155155
}.bind(this));
156-
};
156+
},
157157

158-
this.parseOpera = function parseOpera(e) {
158+
parseOpera: function parseOpera(e) {
159159
if (!e.stacktrace || (e.message.indexOf('\n') > -1 &&
160160
e.message.split('\n').length > e.stacktrace.split('\n').length)) {
161161
return this.parseOpera9(e);
@@ -166,9 +166,9 @@
166166
} else {
167167
return this.parseOpera11(e);
168168
}
169-
};
169+
},
170170

171-
this.parseOpera9 = function parseOpera9(e) {
171+
parseOpera9: function parseOpera9(e) {
172172
var lineRE = /Line (\d+).*script (?:in )?(\S+)/i;
173173
var lines = e.message.split('\n');
174174
var result = [];
@@ -181,9 +181,9 @@
181181
}
182182

183183
return result;
184-
};
184+
},
185185

186-
this.parseOpera10a = function parseOpera10a(e) {
186+
parseOpera10a: function parseOpera10a(e) {
187187
var lineRE = /Line (\d+).*script (?:in )?(\S+)(?:: In function (\S+))?$/i;
188188
var lines = e.stacktrace.split('\n');
189189
var result = [];
@@ -196,12 +196,12 @@
196196
}
197197

198198
return result;
199-
};
199+
},
200200

201201
// Opera 10.65+ Error.stack very similar to FF/Safari
202-
this.parseOpera11 = function parseOpera11(error) {
202+
parseOpera11: function parseOpera11(error) {
203203
return error.stack.split('\n').filter(function (line) {
204-
return !!line.match(this.firefoxSafariStackEntryRegExp);
204+
return !!line.match(FIREFOX_SAFARI_STACK_REGEXP);
205205
}.bind(this)).map(function (line) {
206206
var tokens = line.split('@');
207207
var location = tokens.pop().split(':');
@@ -211,7 +211,7 @@
211211
var args = (argsRaw === undefined || argsRaw === '[arguments not available]') ? undefined : argsRaw.split(',');
212212
return new StackFrame(functionName, args, location[0] + ':' + location[1], location[2], location[3]);
213213
});
214-
};
214+
}
215215
};
216216
}));
217217

spec/error-stack-parser-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global StackFrame: false, ErrorStackParser: false, CapturedExceptions: false */
22
describe('ErrorStackParser', function () {
33
describe('#parse', function () {
4-
var unit = new ErrorStackParser();
4+
var unit = ErrorStackParser;
55
it('should parse Safari 6 Error.stack', function () {
66
var stackFrames = unit.parse(CapturedExceptions.SAFARI_6);
77
expect(stackFrames).toBeTruthy();
@@ -86,4 +86,4 @@ describe('ErrorStackParser', function () {
8686
expect(stackFrames[3]).toMatchStackFrame([undefined, undefined, 'http://path/to/file.js', 15]);
8787
});
8888
});
89-
});
89+
});

0 commit comments

Comments
 (0)