diff --git a/src/raven.js b/src/raven.js index 4c43a402f3d7..558f83af6d3e 100644 --- a/src/raven.js +++ b/src/raven.js @@ -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. @@ -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 @@ -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. diff --git a/test/raven.test.js b/test/raven.test.js index c8ad6bb0ccf3..d9ca66a5caa7 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -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');