Skip to content

Commit 6d2b6cb

Browse files
author
rhart
committed
Make error message optional when reporting
1 parent f812877 commit 6d2b6cb

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Given a function that has been instrumented, revert the function to it's origina
117117

118118
* **fn: Function** - Instrumented Function
119119

120-
#### `StackTrace.report(message, stackframes, url)` => [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)(String)
120+
#### `StackTrace.report(stackframes, url, message)` => [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)(String)
121121
Given an an error message and Array of StackFrames, serialize and POST to given URL. Promise is resolved with response text from POST request.
122122

123123
Example JSON POST data:

spec/stacktrace-spec.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ describe('StackTrace', function() {
192192
jasmine.Ajax.uninstall();
193193
});
194194

195-
it('sends POST request to given URL', function(done) {
195+
it('sends POST request to given URL with a message', function(done) {
196196
var url = 'http://domain.ext/endpoint';
197197
var errorMsg = 'BOOM';
198198
var stackframes = [new StackFrame('fn', undefined, 'file.js', 32, 1)];
199199

200-
StackTrace.report(errorMsg, stackframes, url).then(callback, done.fail)['catch'](done.fail);
200+
StackTrace.report(stackframes, url, errorMsg).then(callback, done.fail)['catch'](done.fail);
201201

202202
var postRequest = jasmine.Ajax.requests.mostRecent();
203203
postRequest.respondWith({status: 201, contentType: 'text/plain', responseText: 'OK'});
@@ -210,13 +210,29 @@ describe('StackTrace', function() {
210210
}
211211
});
212212

213+
it('sends POST request to given URL without a message', function(done) {
214+
var url = 'http://domain.ext/endpoint';
215+
var stackframes = [new StackFrame('fn', undefined, 'file.js', 32, 1)];
216+
217+
StackTrace.report(stackframes, url).then(callback, done.fail)['catch'](done.fail);
218+
219+
var postRequest = jasmine.Ajax.requests.mostRecent();
220+
postRequest.respondWith({status: 201, contentType: 'text/plain', responseText: 'OK'});
221+
222+
function callback() {
223+
expect(postRequest.data()).toEqual({stack: stackframes});
224+
expect(postRequest.method).toBe('post');
225+
expect(postRequest.url).toBe(url);
226+
done();
227+
}
228+
});
229+
213230
it('rejects if POST request fails', function(done) {
214231
var url = 'http://domain.ext/endpoint';
215-
var errorMsg = 'BOOM';
216232
var stackframes = [new StackFrame('fn', undefined, 'file.js', 32, 1)];
217233

218234
jasmine.Ajax.stubRequest(url).andError();
219-
StackTrace.report(errorMsg, stackframes, url).then(done.fail, done)['catch'](done);
235+
StackTrace.report(stackframes, url).then(done.fail, done)['catch'](done);
220236
});
221237
});
222238
});

stacktrace.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@
167167
*
168168
* @param {Array} stackframes
169169
* @param {String} url
170+
* @param {String} errorMsg
170171
*/
171-
report: function StackTrace$$report(errorMsg, stackframes, url) {
172+
report: function StackTrace$$report(stackframes, url, errorMsg) {
172173
return new Promise(function(resolve, reject) {
173174
var req = new XMLHttpRequest();
174175
req.onerror = reject;
@@ -183,7 +184,13 @@
183184
};
184185
req.open('post', url);
185186
req.setRequestHeader('Content-Type', 'application/json');
186-
req.send(JSON.stringify({message: errorMsg, stack: stackframes}));
187+
188+
var reportPayload = {stack: stackframes};
189+
if (errorMsg != undefined) {
190+
reportPayload.message = errorMsg;
191+
}
192+
193+
req.send(JSON.stringify(reportPayload));
187194
});
188195
}
189196
};

0 commit comments

Comments
 (0)