Skip to content

Commit db5fec3

Browse files
bz2steren
authored andcommitted
Add option to skip more frames using report (#58)
New options parameter on report() for per-call config. Add skipLocalFrames option to trim the head of a locally generated stacktrace to remove more than one level. Enforce valid jsdoc using eslint and add params for test helpers. Add documentation for passing options to report().
1 parent ca5e167 commit db5fec3

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
}],
4747
"spaced-comment": ["error", "always"],
4848
"switch-colon-spacing": "error",
49-
"unicode-bom": ["error", "never"]
49+
"unicode-bom": ["error", "never"],
50+
"valid-jsdoc": ["error", {
51+
"requireReturn": false
52+
}]
5053
},
5154
"overrides": [{
5255
"files": ["test/*.js"],

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ window.addEventListener('DOMContentLoaded', function() {
7979
version: '<my-service-version>', // (optional)
8080
// reportUncaughtExceptions: false // (optional) Set to false to stop reporting unhandled exceptions.
8181
// reportUnhandledPromiseRejections: false // (optional) Set to false to stop reporting unhandled promise rejections.
82-
// disabled: true // (optional) Set to true to not report errors when calling report(), this can be used when developping locally.
82+
// disabled: true // (optional) Set to true to not report errors when calling report(), this can be used when developing locally.
8383
// context: {user: 'user1'} // (optional) You can set the user later using setUser()
8484
});
8585
});
@@ -228,13 +228,28 @@ Consumption of the errorHandlerUtility would essentially follow the following pa
228228
// MyComponent.jsx
229229
import errorHandler from './errorHandlerUtility';
230230

231-
// Some example code that throws an error
232-
.catch(error) {
231+
try {
232+
someFunctionThatThrows();
233+
} catch (error) {
233234
errorHandler.report(error);
234235
}
235236

236237
```
237238

239+
If the call to report has additional levels of wrapping code, extra
240+
frames can be trimmed from the top of generated stacks by using a
241+
number greater than one for the `skipLocalFrames` option:
242+
243+
```javascript
244+
import errorHandler from './errorHandlerUtility';
245+
246+
function backendReport (string) {
247+
// Skipping the two frames, for report() and for backendReport()
248+
errorHandler.report(error, {skipLocalFrames: 2});
249+
}
250+
251+
```
252+
238253
## FAQ
239254

240255
**Q: Should I use this code in my production application?** A: This is an experimental library provided without any guarantee or official support. We do not recommend using it on production without performing a review of its code.

stackdriver-errors.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,18 @@
9393
/**
9494
* Report an error to the Stackdriver Error Reporting API
9595
* @param {Error|String} err - The Error object or message string to report.
96+
* @param {Object} options - Configuration for this report.
97+
* @param {number} [options.skipLocalFrames=1] - Omit number of frames if creating stack.
9698
* @returns {Promise} A promise that completes when the report has been sent.
9799
*/
98-
StackdriverErrorReporter.prototype.report = function(err) {
100+
StackdriverErrorReporter.prototype.report = function(err, options) {
99101
if (this.disabled) {
100102
return Promise.resolve(null);
101103
}
102104
if (!err) {
103105
return Promise.reject(new Error('no error to report'));
104106
}
107+
options = options || {};
105108

106109
var payload = {};
107110
payload.serviceContext = this.serviceContext;
@@ -120,7 +123,7 @@
120123
err = e;
121124
}
122125
// the first frame when using report() is always this library
123-
firstFrameIndex = 1;
126+
firstFrameIndex = options.skipLocalFrames || 1;
124127
}
125128
var that = this;
126129
// This will use sourcemaps and normalize the stack frames

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var WAIT_FOR_STACKTRACE_FROMERROR = 15;
2121

2222
/**
2323
* Helper function testing if a given message has been reported
24+
* @param {string} [message] - Substring that message must contain.
2425
*/
2526
function expectRequestWithMessage(message) {
2627
expect(requests.length).to.equal(1);
@@ -31,6 +32,7 @@ function expectRequestWithMessage(message) {
3132

3233
/**
3334
* Helper for testing call stack reporting
35+
* @param {string} [message] - Contents of error to throw.
3436
*/
3537
function throwError(message) {
3638
throw new TypeError(message);
@@ -129,6 +131,26 @@ describe('Reporting errors', function() {
129131
});
130132
});
131133

134+
it('should include report origin by default', function() {
135+
var helper = function helperFn(handler) {
136+
return handler.report('common message');
137+
};
138+
return helper(errorHandler).then(function() {
139+
expectRequestWithMessage(': common message\n at helperFn (');
140+
});
141+
});
142+
143+
it('should skip number of frames if option is given', function() {
144+
var helper = function outerFn(handler) {
145+
return (function innerFn() {
146+
return handler.report('common message', {skipLocalFrames: 2});
147+
})();
148+
};
149+
return helper(errorHandler).then(function() {
150+
expectRequestWithMessage(': common message\n at outerFn (');
151+
});
152+
});
153+
132154
it('should extract and send stack traces from Errors', function() {
133155
var message = 'custom message';
134156
// Throw and catch error to attach a stacktrace

0 commit comments

Comments
 (0)