Skip to content

Commit

Permalink
Give clear results when report promise completes
Browse files Browse the repository at this point in the history
If the XMLHttpRequest fails or a non-2XX code results the promise
will be rejected with an Error. Otherwise it will resolve with the
message reported included.

Fixes #32
  • Loading branch information
bz2 committed Dec 7, 2018
1 parent 3b069de commit 2f1bed6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
39 changes: 27 additions & 12 deletions stackdriver-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,30 @@
var that = this;
// This will use sourcemaps and normalize the stack frames
// eslint-disable-next-line no-undef
return StackTrace.fromError(err).then(function(stack){
payload.message = err.toString();
return StackTrace.fromError(err).then(function(stack) {
var lines = [err.toString()];
// Reconstruct to a JS stackframe as expected by Error Reporting parsers.
for(var s = firstFrameIndex; s < stack.length; s++) {
payload.message += '\n';
// Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers.
// stack[s].source should not be used because not populated when created from source map.
//
// If functionName or methodName isn't available <anonymous> will be used as the name.
payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
// Cannot use stack[s].source as it is not populated from source maps.
lines.push([
' at ',
// If a function name is not available '<anonymous>' will be used.
stack[s].getFunctionName() || '<anonymous>', ' (',
stack[s].getFileName(), ':',
stack[s].getLineNumber(), ':',
stack[s].getColumnNumber(), ')',
].join(''));
}
return that.sendErrorPayload(payload);
return lines.join('\n');
}, function(reason) {
// Failure to extract stacktrace
payload.message = [
return [
'Error extracting stack trace: ', reason, '\n',
err.toString(), '\n',
' (', err.file, ':', err.line, ':', err.column, ')',
].join('');
}).then(function(message) {
payload.message = message;
return that.sendErrorPayload(payload);
});
};
Expand All @@ -156,8 +162,17 @@
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

return new Promise(function(resolve, reject) {
xhr.onloadend = resolve;
xhr.onerror = reject;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var code = xhr.status;
if (code >= 200 && code < 300) {
resolve({message: payload.message});
} else {
var condition = code ? code + ' http response' : 'network error';
reject(new Error(condition + ' on stackdriver report'));
}
}
};
xhr.send(JSON.stringify(payload));
});
};
Expand Down
20 changes: 17 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ describe('Reporting errors', function () {
}
});

it('should resolve with stacktrace in message', function () {
try {
throwError('mystery problem');
} catch (e) {
return errorHandler.report(e).then(function(details) {
var expected = ': mystery problem\n at throwError (';
expectRequestWithMessage(expected);
expect(details.message).to.contain(expected);
});
}
});

describe('XHR error handling', function() {
it('should handle network error', function () {
requestHandler = function (req) {
Expand All @@ -172,10 +184,9 @@ describe('Reporting errors', function () {
var message = 'News that will fail to send';
return errorHandler.report(message).then(function() {
throw new Error('unexpected fulfilled report');
}, function(e) {
}, function(err) {
expectRequestWithMessage(message);
// TODO: Expose a tidied up error object
expect(e.target.status).to.equal(0);
expect(err.message).to.equal('network error on stackdriver report');
});
});

Expand All @@ -186,7 +197,10 @@ describe('Reporting errors', function () {
errorHandler.start({key:'key', projectId:'projectId'});
var message = 'News that was rejected on send';
return errorHandler.report(message).then(function() {
throw new Error('unexpected fulfilled report');
}, function(err) {
expectRequestWithMessage(message);
expect(err.message).to.equal('503 http response on stackdriver report');
});
});
});
Expand Down

0 comments on commit 2f1bed6

Please sign in to comment.