Skip to content

Commit fbe377e

Browse files
committed
Can pass dsn to showReportDialog, add tests
1 parent 26e7a83 commit fbe377e

File tree

2 files changed

+105
-16
lines changed

2 files changed

+105
-16
lines changed

src/raven.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,8 @@ Raven.prototype = {
123123
this._globalKey = uri.user;
124124
this._globalProject = uri.path.substr(lastSlash + 1);
125125

126-
// assemble the endpoint from the uri pieces
127-
this._globalServer = '//' + uri.host +
128-
(uri.port ? ':' + uri.port : '');
126+
this._globalServer = this._getGlobalServer(uri);
129127

130-
if (uri.protocol) {
131-
this._globalServer = uri.protocol + ':' + this._globalServer;
132-
}
133128
this._globalEndpoint = this._globalServer +
134129
'/' + path + 'api/' + this._globalProject + '/store/';
135130

@@ -508,26 +503,32 @@ Raven.prototype = {
508503
options = options || {};
509504

510505
var lastEventId = options.eventId || this.lastEventId();
511-
if (!lastEventId)
512-
return;
506+
if (!lastEventId) {
507+
throw new RavenConfigError('Missing eventId');
508+
}
509+
510+
var dsn = options.dsn || this._dsn;
511+
if (!dsn) {
512+
throw new RavenConfigError('Missing DSN');
513+
}
513514

514515
var encode = encodeURIComponent;
515516
var qs = '';
516517
qs += '?eventId=' + encode(lastEventId);
517-
qs += '&dsn=' + encode(this._dsn || '');
518+
qs += '&dsn=' + encode(dsn);
518519

519-
var user = this._globalContext.user;
520+
var user = options.user || this._globalContext.user;
520521
if (user) {
521-
if (user.name)
522-
qs += '&name=' + encode(user.name);
523-
if (user.email)
524-
qs += '&email=' + encode(user.email);
522+
if (user.name) qs += '&name=' + encode(user.name);
523+
if (user.email) qs += '&email=' + encode(user.email);
525524
}
526525

526+
var globalServer = this._getGlobalServer(this._parseDSN(dsn));
527+
527528
var script = document.createElement('script');
528529
script.async = true;
529-
script.src = this._globalServer + '/api/embed/error-page/' + qs;
530-
document.getElementsByTagName('body')[0].appendChild(script);
530+
script.src = globalServer + '/api/embed/error-page/' + qs;
531+
(document.head || document.body).appendChild(script);
531532
},
532533

533534
/**** Private functions ****/
@@ -715,6 +716,17 @@ Raven.prototype = {
715716
return dsn;
716717
},
717718

719+
_getGlobalServer: function(uri) {
720+
// assemble the endpoint from the uri pieces
721+
var globalServer = '//' + uri.host +
722+
(uri.port ? ':' + uri.port : '');
723+
724+
if (uri.protocol) {
725+
globalServer = uri.protocol + ':' + globalServer;
726+
}
727+
return globalServer;
728+
},
729+
718730
_handleOnErrorStackInfo: function() {
719731
// if we are intentionally ignoring errors via onerror, bail out
720732
if (!this._ignoreOnError) {

test/raven.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var proxyquire = require('proxyquireify')(require);
66

77
var TraceKit = require('../vendor/TraceKit/tracekit');
8+
89
var _Raven = proxyquire('../src/raven', {
910
'./utils': {
1011
// patched to return a predictable result
@@ -2028,4 +2029,80 @@ describe('Raven (public API)', function() {
20282029
assert.isFalse(Raven.isSetup());
20292030
});
20302031
});
2032+
2033+
describe('.showReportDialog', function () {
2034+
it('should throw a RavenConfigError if no eventId', function () {
2035+
assert.throws(function () {
2036+
Raven.showReportDialog({
2037+
dsn: SENTRY_DSN // dsn specified via options
2038+
});
2039+
}, 'Missing eventId');
2040+
2041+
Raven.config(SENTRY_DSN);
2042+
assert.throws(function () {
2043+
Raven.showReportDialog(); // dsn specified via Raven.config
2044+
}, 'Missing eventId');
2045+
});
2046+
2047+
it('should throw a RavenConfigError if no dsn', function () {
2048+
assert.throws(function () {
2049+
Raven.showReportDialog({
2050+
eventId: 'abc123'
2051+
});
2052+
}, 'Missing DSN');
2053+
});
2054+
2055+
describe('script tag insertion', function () {
2056+
beforeEach(function () {
2057+
this.appendChildStub = this.sinon.stub(document.head, 'appendChild');
2058+
});
2059+
2060+
it('should specify embed API endpoint and basic query string (DSN, eventId)', function () {
2061+
Raven.showReportDialog({
2062+
eventId: 'abc123',
2063+
dsn: SENTRY_DSN
2064+
});
2065+
2066+
var script = this.appendChildStub.getCall(0).args[0];
2067+
assert.equal(script.src, 'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2');
2068+
2069+
this.appendChildStub.reset();
2070+
2071+
Raven
2072+
.config(SENTRY_DSN)
2073+
.captureException(new Error('foo')) // generates lastEventId
2074+
.showReportDialog();
2075+
2076+
this.appendChildStub.getCall(0).args[0];
2077+
assert.equal(script.src, 'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2');
2078+
});
2079+
2080+
it('should specify embed API endpoint and full query string (DSN, eventId, user)', function () {
2081+
Raven.showReportDialog({
2082+
eventId: 'abc123',
2083+
dsn: SENTRY_DSN,
2084+
user: {
2085+
name: 'Average Normalperson',
2086+
email: 'an@example.com'
2087+
}
2088+
});
2089+
2090+
var script = this.appendChildStub.getCall(0).args[0];
2091+
assert.equal(script.src, 'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2&name=Average%20Normalperson&email=an%40example.com');
2092+
2093+
this.appendChildStub.reset();
2094+
Raven
2095+
.config(SENTRY_DSN)
2096+
.captureException(new Error('foo')) // generates lastEventId
2097+
.setUserContext({
2098+
name: 'Average Normalperson 2',
2099+
email: 'an2@example.com'
2100+
})
2101+
.showReportDialog();
2102+
2103+
var script = this.appendChildStub.getCall(0).args[0];
2104+
assert.equal(script.src, 'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2&name=Average%20Normalperson%202&email=an2%40example.com');
2105+
});
2106+
});
2107+
});
20312108
});

0 commit comments

Comments
 (0)