Skip to content

Commit 6539718

Browse files
committed
Merge pull request #233 from TvoroG/raven-config
Do not allow to call Raven.config() twice
2 parents 3933cb4 + dc5d36c commit 6539718

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

src/raven.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var _Raven = window.Raven,
2121
tags: {},
2222
extra: {}
2323
},
24-
authQueryString;
24+
authQueryString,
25+
isRavenInstalled = false;
2526

2627
/*
2728
* The core Raven singleton
@@ -52,6 +53,10 @@ var Raven = {
5253
* @return {Raven}
5354
*/
5455
config: function(dsn, options) {
56+
if (globalServer) {
57+
logDebug('error', 'Error: Raven has already been configured');
58+
return Raven;
59+
}
5560
if (!dsn) return Raven;
5661

5762
var uri = parseDSN(dsn),
@@ -117,8 +122,9 @@ var Raven = {
117122
* @return {Raven}
118123
*/
119124
install: function() {
120-
if (isSetup()) {
125+
if (isSetup() && !isRavenInstalled) {
121126
TraceKit.report.subscribe(handleStackInfo);
127+
isRavenInstalled = true;
122128
}
123129

124130
return Raven;
@@ -212,6 +218,7 @@ var Raven = {
212218
*/
213219
uninstall: function() {
214220
TraceKit.report.uninstall();
221+
isRavenInstalled = false;
215222

216223
return Raven;
217224
},
@@ -681,9 +688,7 @@ function makeRequest(data) {
681688
function isSetup() {
682689
if (!hasJSON) return false; // needs JSON support
683690
if (!globalServer) {
684-
if (window.console && console.error && Raven.debug) {
685-
console.error("Error: Raven has not been configured.");
686-
}
691+
logDebug('error', 'Error: Raven has not been configured.');
687692
return false;
688693
}
689694
return true;
@@ -720,6 +725,12 @@ function uuid4() {
720725
});
721726
}
722727

728+
function logDebug(level, message) {
729+
if (window.console && console[level] && Raven.debug) {
730+
console[level](message);
731+
}
732+
}
733+
723734
function afterLoad() {
724735
// Attempt to initialize Raven on load
725736
var RavenConfig = window.RavenConfig;

test/raven.test.js

+43-18
Original file line numberDiff line numberDiff line change
@@ -218,31 +218,32 @@ describe('globals', function() {
218218
it('should return false when Raven is not configured', function() {
219219
hasJSON = true; // be explicit
220220
globalServer = undefined;
221-
this.sinon.stub(console, 'error');
221+
this.sinon.stub(window, 'logDebug');
222222
assert.isFalse(isSetup());
223223
});
224224

225-
it('should not write to console.error when Raven is not configured and Raven.debug is false', function() {
226-
hasJSON = true; // be explicit
227-
globalServer = undefined;
228-
Raven.debug = false;
229-
this.sinon.stub(console, 'error');
230-
isSetup();
231-
assert.isFalse(console.error.calledOnce);
225+
it('should return true when everything is all gravy', function() {
226+
hasJSON = true;
227+
assert.isTrue(isSetup());
232228
});
229+
});
233230

234-
it('should write to console.error when Raven is not configured and Raven.debug is true', function() {
235-
hasJSON = true; // be explicit
236-
globalServer = undefined;
237-
Raven.debug = true;
238-
this.sinon.stub(console, 'error');
239-
isSetup();
240-
assert.isTrue(console.error.calledOnce);
231+
describe('logDebug', function() {
232+
var level = 'error',
233+
message = 'foobar';
234+
235+
it('should not write to console when Raven.debug is false', function() {
236+
Raven.debug = false;
237+
this.sinon.stub(console, level);
238+
logDebug(level, message);
239+
assert.isFalse(console[level].called);
241240
});
242241

243-
it('should return true when everything is all gravy', function() {
244-
hasJSON = true;
245-
assert.isTrue(isSetup());
242+
it('should write to console when Raven.debug is true', function() {
243+
Raven.debug = true;
244+
this.sinon.stub(console, level);
245+
logDebug(level, message);
246+
assert.isTrue(console[level].calledOnce);
246247
});
247248
});
248249

@@ -1168,6 +1169,15 @@ describe('Raven (public API)', function() {
11681169
assert.equal(Raven.config(''), Raven);
11691170
});
11701171

1172+
it('should not set global options more than once', function() {
1173+
this.sinon.spy(window, 'parseDSN');
1174+
this.sinon.stub(window, 'logDebug');
1175+
setupRaven();
1176+
setupRaven();
1177+
assert.isTrue(parseDSN.calledOnce);
1178+
assert.isTrue(logDebug.called);
1179+
});
1180+
11711181
describe('whitelistUrls', function() {
11721182
it('should be false if none are passed', function() {
11731183
Raven.config('//[email protected]/2');
@@ -1237,6 +1247,14 @@ describe('Raven (public API)', function() {
12371247
assert.isTrue(TraceKit.report.subscribe.calledOnce);
12381248
assert.equal(TraceKit.report.subscribe.lastCall.args[0], handleStackInfo);
12391249
});
1250+
1251+
it('should not register itself more than once', function() {
1252+
this.sinon.stub(window, 'isSetup').returns(true);
1253+
this.sinon.stub(TraceKit.report, 'subscribe');
1254+
Raven.install();
1255+
Raven.install();
1256+
assert.isTrue(TraceKit.report.subscribe.calledOnce);
1257+
});
12401258
});
12411259

12421260
describe('.wrap', function() {
@@ -1382,6 +1400,13 @@ describe('Raven (public API)', function() {
13821400
Raven.uninstall();
13831401
assert.isTrue(TraceKit.report.uninstall.calledOnce);
13841402
});
1403+
1404+
it('should set isRavenInstalled flag to false', function() {
1405+
isRavenInstalled = true;
1406+
this.sinon.stub(TraceKit.report, 'uninstall');
1407+
Raven.uninstall();
1408+
assert.isFalse(isRavenInstalled);
1409+
});
13851410
});
13861411

13871412
describe('.setUserContext', function() {

0 commit comments

Comments
 (0)