Skip to content

Commit fe7b594

Browse files
committed
Merge pull request #379 from getsentry/refactor
Refactor makeRequest to prepare to be overridden
2 parents 7040cb9 + 2e4a92c commit fe7b594

File tree

2 files changed

+148
-76
lines changed

2 files changed

+148
-76
lines changed

src/raven.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var _Raven = window.Raven,
2323
maxMessageLength: 100,
2424
extra: {}
2525
},
26-
authQueryString,
2726
isRavenInstalled = false,
2827
objectPrototype = Object.prototype,
2928
// capture references to window.console *and* all its methods first
@@ -114,8 +113,6 @@ var Raven = {
114113

115114
TraceKit.collectWindowErrors = !!globalOptions.collectWindowErrors;
116115

117-
setAuthQueryString();
118-
119116
// return for chaining
120117
return Raven;
121118
},
@@ -508,15 +505,6 @@ function each(obj, callback) {
508505
}
509506
}
510507

511-
512-
function setAuthQueryString() {
513-
authQueryString =
514-
'?sentry_version=4' +
515-
'&sentry_client=raven-js/' + Raven.VERSION +
516-
'&sentry_key=' + globalKey;
517-
}
518-
519-
520508
function handleStackInfo(stackInfo, options) {
521509
var frames = [];
522510

@@ -748,35 +736,46 @@ function send(data) {
748736
// Set lastEventId after we know the error should actually be sent
749737
lastEventId = data.event_id || (data.event_id = uuid4());
750738

751-
makeRequest(data);
752-
}
739+
logDebug('debug', 'Raven about to send:', data);
753740

741+
if (!isSetup()) return;
754742

755-
function makeRequest(data) {
756-
var img,
757-
src;
743+
makeRequest({
744+
url: globalServer,
745+
auth: {
746+
sentry_version: '4',
747+
sentry_client: 'raven-js/' + Raven.VERSION,
748+
sentry_key: globalKey
749+
},
750+
data: data,
751+
options: globalOptions,
752+
onSuccess: function success() {
753+
triggerEvent('success', {
754+
data: data,
755+
src: globalServer
756+
});
757+
},
758+
onError: function failure() {
759+
triggerEvent('failure', {
760+
data: data,
761+
src: globalServer
762+
});
763+
}
764+
});
765+
}
758766

759-
logDebug('debug', 'Raven about to send:', data);
767+
function makeRequest(opts) {
768+
// Tack on sentry_data to auth options, which get urlencoded
769+
opts.auth.sentry_data = JSON.stringify(opts.data);
760770

761-
if (!isSetup()) return;
771+
var img = newImage(),
772+
src = opts.url + '?' + urlencode(opts.auth);
762773

763-
img = newImage();
764-
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
765-
if (globalOptions.crossOrigin || globalOptions.crossOrigin === '') {
766-
img.crossOrigin = globalOptions.crossOrigin;
774+
if (opts.options.crossOrigin || opts.options.crossOrigin === '') {
775+
img.crossOrigin = opts.options.crossOrigin;
767776
}
768-
img.onload = function success() {
769-
triggerEvent('success', {
770-
data: data,
771-
src: src
772-
});
773-
};
774-
img.onerror = img.onabort = function failure() {
775-
triggerEvent('failure', {
776-
data: data,
777-
src: src
778-
});
779-
};
777+
img.onload = opts.onSuccess;
778+
img.onerror = img.onabort = opts.onError;
780779
img.src = src;
781780
}
782781

@@ -870,4 +869,13 @@ function afterLoad() {
870869
Raven.config(RavenConfig.dsn, RavenConfig.config).install();
871870
}
872871
}
872+
873+
function urlencode(o) {
874+
var pairs = [];
875+
each(o, function(key, value) {
876+
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
877+
});
878+
return pairs.join('&');
879+
}
880+
873881
afterLoad();

0 commit comments

Comments
 (0)