Skip to content

Commit c474a32

Browse files
mike-zornbenvinegar
authored andcommitted
Allow changing the DSN after configuration (#706)
Addresses #650.
1 parent e8040b1 commit c474a32

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/raven.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ Raven.prototype = {
123123
});
124124
}
125125

126-
var uri = this._parseDSN(dsn),
127-
lastSlash = uri.path.lastIndexOf('/'),
128-
path = uri.path.substr(1, lastSlash);
129-
130-
this._dsn = dsn;
126+
this.setDSN(dsn);
131127

132128
// "Script error." is hard coded into browsers for errors that it can't read.
133129
// this is the result of a script being pulled in from an external domain and CORS.
@@ -156,15 +152,6 @@ Raven.prototype = {
156152
}
157153
this._globalOptions.autoBreadcrumbs = autoBreadcrumbs;
158154

159-
this._globalKey = uri.user;
160-
this._globalSecret = uri.pass && uri.pass.substr(1);
161-
this._globalProject = uri.path.substr(lastSlash + 1);
162-
163-
this._globalServer = this._getGlobalServer(uri);
164-
165-
this._globalEndpoint = this._globalServer +
166-
'/' + path + 'api/' + this._globalProject + '/store/';
167-
168155
TraceKit.collectWindowErrors = !!this._globalOptions.collectWindowErrors;
169156

170157
// return for chaining
@@ -199,6 +186,27 @@ Raven.prototype = {
199186
return this;
200187
},
201188

189+
/*
190+
* Set the DSN (can be called multiple time unlike config)
191+
*
192+
* @param {string} dsn The public Sentry DSN
193+
*/
194+
setDSN: function(dsn) {
195+
var uri = this._parseDSN(dsn),
196+
lastSlash = uri.path.lastIndexOf('/'),
197+
path = uri.path.substr(1, lastSlash);
198+
199+
this._dsn = dsn;
200+
this._globalKey = uri.user;
201+
this._globalSecret = uri.pass && uri.pass.substr(1);
202+
this._globalProject = uri.path.substr(lastSlash + 1);
203+
204+
this._globalServer = this._getGlobalServer(uri);
205+
206+
this._globalEndpoint = this._globalServer +
207+
'/' + path + 'api/' + this._globalProject + '/store/';
208+
},
209+
202210
/*
203211
* Wrap code within a context so Raven can capture errors
204212
* reliably across domains that is executed immediately.

test/raven.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,18 @@ describe('Raven (public API)', function() {
14601460
});
14611461
});
14621462

1463+
describe('.setDSN', function() {
1464+
it('should work with a DSN after Raven has been configured', function() {
1465+
Raven.config('//[email protected]/3');
1466+
Raven.setDSN(SENTRY_DSN)
1467+
1468+
assert.equal(Raven._globalKey, 'abc');
1469+
assert.equal(Raven._globalSecret, '');
1470+
assert.equal(Raven._globalEndpoint, 'http://example.com:80/api/2/store/');
1471+
assert.equal(Raven._globalProject, '2');
1472+
});
1473+
});
1474+
14631475
describe('.config', function() {
14641476
it('should work with a DSN', function() {
14651477
assert.equal(Raven, Raven.config(SENTRY_DSN, {foo: 'bar'}), 'it should return Raven');

0 commit comments

Comments
 (0)