Skip to content

Commit ca5e167

Browse files
bz2steren
authored andcommitted
Give clear results when report promise completes (#59)
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
1 parent c95e5b3 commit ca5e167

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

stackdriver-errors.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,29 @@
126126
// This will use sourcemaps and normalize the stack frames
127127
// eslint-disable-next-line no-undef
128128
return StackTrace.fromError(err).then(function(stack) {
129-
payload.message = err.toString();
129+
var lines = [err.toString()];
130+
// Reconstruct to a JS stackframe as expected by Error Reporting parsers.
130131
for (var s = firstFrameIndex; s < stack.length; s++) {
131-
payload.message += '\n';
132-
// Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers.
133-
// stack[s].source should not be used because not populated when created from source map.
134-
//
135-
// If functionName or methodName isn't available <anonymous> will be used as the name.
136-
payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber(), ':', stack[s].getColumnNumber(), ')'].join('');
132+
// Cannot use stack[s].source as it is not populated from source maps.
133+
lines.push([
134+
' at ',
135+
// If a function name is not available '<anonymous>' will be used.
136+
stack[s].getFunctionName() || '<anonymous>', ' (',
137+
stack[s].getFileName(), ':',
138+
stack[s].getLineNumber(), ':',
139+
stack[s].getColumnNumber(), ')',
140+
].join(''));
137141
}
138-
return that.sendErrorPayload(payload);
142+
return lines.join('\n');
139143
}, function(reason) {
140144
// Failure to extract stacktrace
141-
payload.message = [
145+
return [
142146
'Error extracting stack trace: ', reason, '\n',
143147
err.toString(), '\n',
144148
' (', err.file, ':', err.line, ':', err.column, ')',
145149
].join('');
150+
}).then(function(message) {
151+
payload.message = message;
146152
return that.sendErrorPayload(payload);
147153
});
148154
};
@@ -156,8 +162,17 @@
156162
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
157163

158164
return new Promise(function(resolve, reject) {
159-
xhr.onloadend = resolve;
160-
xhr.onerror = reject;
165+
xhr.onreadystatechange = function() {
166+
if (xhr.readyState === 4) {
167+
var code = xhr.status;
168+
if (code >= 200 && code < 300) {
169+
resolve({message: payload.message});
170+
} else {
171+
var condition = code ? code + ' http response' : 'network error';
172+
reject(new Error(condition + ' on stackdriver report'));
173+
}
174+
}
175+
};
161176
xhr.send(JSON.stringify(payload));
162177
});
163178
};

test/test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ describe('Reporting errors', function() {
167167
}
168168
});
169169

170+
it('should resolve with stacktrace in message', function() {
171+
try {
172+
throwError('mystery problem');
173+
} catch (e) {
174+
return errorHandler.report(e).then(function(details) {
175+
var expected = ': mystery problem\n at throwError (';
176+
expectRequestWithMessage(expected);
177+
expect(details.message).to.contain(expected);
178+
});
179+
}
180+
});
181+
170182
describe('XHR error handling', function() {
171183
it('should handle network error', function() {
172184
requestHandler = function(req) {
@@ -175,10 +187,9 @@ describe('Reporting errors', function() {
175187
var message = 'News that will fail to send';
176188
return errorHandler.report(message).then(function() {
177189
throw new Error('unexpected fulfilled report');
178-
}, function(e) {
190+
}, function(err) {
179191
expectRequestWithMessage(message);
180-
// TODO: Expose a tidied up error object
181-
expect(e.target.status).to.equal(0);
192+
expect(err.message).to.equal('network error on stackdriver report');
182193
});
183194
});
184195

@@ -189,7 +200,10 @@ describe('Reporting errors', function() {
189200
errorHandler.start({key: 'key', projectId: 'projectId'});
190201
var message = 'News that was rejected on send';
191202
return errorHandler.report(message).then(function() {
203+
throw new Error('unexpected fulfilled report');
204+
}, function(err) {
192205
expectRequestWithMessage(message);
206+
expect(err.message).to.equal('503 http response on stackdriver report');
193207
});
194208
});
195209
});

0 commit comments

Comments
 (0)