Skip to content

Commit

Permalink
Allow changing the DSN after configuration (#706)
Browse files Browse the repository at this point in the history
Addresses #650.
  • Loading branch information
mike-zorn authored and benvinegar committed Aug 30, 2016
1 parent e8040b1 commit c474a32
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ Raven.prototype = {
});
}

var uri = this._parseDSN(dsn),
lastSlash = uri.path.lastIndexOf('/'),
path = uri.path.substr(1, lastSlash);

this._dsn = dsn;
this.setDSN(dsn);

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

this._globalKey = uri.user;
this._globalSecret = uri.pass && uri.pass.substr(1);
this._globalProject = uri.path.substr(lastSlash + 1);

this._globalServer = this._getGlobalServer(uri);

this._globalEndpoint = this._globalServer +
'/' + path + 'api/' + this._globalProject + '/store/';

TraceKit.collectWindowErrors = !!this._globalOptions.collectWindowErrors;

// return for chaining
Expand Down Expand Up @@ -199,6 +186,27 @@ Raven.prototype = {
return this;
},

/*
* Set the DSN (can be called multiple time unlike config)
*
* @param {string} dsn The public Sentry DSN
*/
setDSN: function(dsn) {
var uri = this._parseDSN(dsn),
lastSlash = uri.path.lastIndexOf('/'),
path = uri.path.substr(1, lastSlash);

this._dsn = dsn;
this._globalKey = uri.user;
this._globalSecret = uri.pass && uri.pass.substr(1);
this._globalProject = uri.path.substr(lastSlash + 1);

this._globalServer = this._getGlobalServer(uri);

this._globalEndpoint = this._globalServer +
'/' + path + 'api/' + this._globalProject + '/store/';
},

/*
* Wrap code within a context so Raven can capture errors
* reliably across domains that is executed immediately.
Expand Down
12 changes: 12 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,18 @@ describe('Raven (public API)', function() {
});
});

describe('.setDSN', function() {
it('should work with a DSN after Raven has been configured', function() {
Raven.config('//def@lol.com/3');
Raven.setDSN(SENTRY_DSN)

assert.equal(Raven._globalKey, 'abc');
assert.equal(Raven._globalSecret, '');
assert.equal(Raven._globalEndpoint, 'http://example.com:80/api/2/store/');
assert.equal(Raven._globalProject, '2');
});
});

describe('.config', function() {
it('should work with a DSN', function() {
assert.equal(Raven, Raven.config(SENTRY_DSN, {foo: 'bar'}), 'it should return Raven');
Expand Down

0 comments on commit c474a32

Please sign in to comment.