Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give clear results when report promise completes #59

Merged
merged 1 commit into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions stackdriver-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,29 @@
// 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();
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 @@ -167,6 +167,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 @@ -175,10 +187,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 @@ -189,7 +200,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