Skip to content

Commit 26e7a83

Browse files
committed
Add Raven.showReportDialog (experimental)
1 parent 6d5a919 commit 26e7a83

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

example/index.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
<head>
44
<title>Scratch Disk</title>
55
</head>
6-
<script src="../vendor/TraceKit/tracekit.js"></script>
7-
<script src="../src/raven.js"></script>
6+
<script src="../dist/raven.js"></script>
87
<!-- <script src="scratch.min.js"></script> -->
98
<script src="scratch.js" crossorigin></script>
109
<script src="file.min.js" crossorigin></script>
@@ -14,7 +13,8 @@
1413
//awesome
1514
Raven.config('http://50dbe04cd1224d439e9c49bf1d0464df@localhost:8000/1', {
1615
whitelistUrls: [
17-
/localhost/
16+
/localhost/,
17+
/127\.0\.0\.1/
1818
],
1919
dataCallback: function(data) {
2020
console.log(data);
@@ -25,7 +25,8 @@
2525
Raven.setUserContext({
2626
2727
id: 5
28-
})
28+
});
29+
2930

3031
</script>
3132
<body>
@@ -36,6 +37,7 @@
3637
<button onclick="derp()">window.onerror</button>
3738
<button onclick="testOptions()">test options</button>
3839
<button onclick="throwString()">throw string</button>
40+
<button onclick="showDialog()">show dialog</button>
3941

4042
</body>
4143
</html>

example/scratch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ function testOptions() {
4040
function throwString() {
4141
throw 'oops';
4242
}
43+
44+
function showDialog() {
45+
broken();
46+
Raven.showReportDialog();
47+
}

src/raven.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Raven.prototype = {
107107
});
108108
}
109109

110+
this._dsn = dsn;
111+
110112
// "Script error." is hard coded into browsers for errors that it can't read.
111113
// this is the result of a script being pulled in from an external domain and CORS.
112114
this._globalOptions.ignoreErrors.push(/^Script error\.?$/);
@@ -123,12 +125,13 @@ Raven.prototype = {
123125

124126
// assemble the endpoint from the uri pieces
125127
this._globalServer = '//' + uri.host +
126-
(uri.port ? ':' + uri.port : '') +
127-
'/' + path + 'api/' + this._globalProject + '/store/';
128+
(uri.port ? ':' + uri.port : '');
128129

129130
if (uri.protocol) {
130131
this._globalServer = uri.protocol + ':' + this._globalServer;
131132
}
133+
this._globalEndpoint = this._globalServer +
134+
'/' + path + 'api/' + this._globalProject + '/store/';
132135

133136
if (this._globalOptions.fetchContext) {
134137
TraceKit.remoteFetching = true;
@@ -498,6 +501,35 @@ Raven.prototype = {
498501
}
499502
},
500503

504+
showReportDialog: function (options) {
505+
if (!window.document) // doesn't work without a document (React native)
506+
return;
507+
508+
options = options || {};
509+
510+
var lastEventId = options.eventId || this.lastEventId();
511+
if (!lastEventId)
512+
return;
513+
514+
var encode = encodeURIComponent;
515+
var qs = '';
516+
qs += '?eventId=' + encode(lastEventId);
517+
qs += '&dsn=' + encode(this._dsn || '');
518+
519+
var user = this._globalContext.user;
520+
if (user) {
521+
if (user.name)
522+
qs += '&name=' + encode(user.name);
523+
if (user.email)
524+
qs += '&email=' + encode(user.email);
525+
}
526+
527+
var script = document.createElement('script');
528+
script.async = true;
529+
script.src = this._globalServer + '/api/embed/error-page/' + qs;
530+
document.getElementsByTagName('body')[0].appendChild(script);
531+
},
532+
501533
/**** Private functions ****/
502534
_ignoreNextOnError: function () {
503535
var self = this;
@@ -933,8 +965,9 @@ Raven.prototype = {
933965

934966
if (!this.isSetup()) return;
935967

968+
var url = this._globalEndpoint;
936969
(globalOptions.transport || this._makeRequest).call(this, {
937-
url: this._globalServer,
970+
url: url,
938971
auth: {
939972
sentry_version: '7',
940973
sentry_client: 'raven-js/' + this.VERSION,
@@ -945,13 +978,13 @@ Raven.prototype = {
945978
onSuccess: function success() {
946979
self._triggerEvent('success', {
947980
data: data,
948-
src: self._globalServer
981+
src: url
949982
});
950983
},
951984
onError: function failure() {
952985
self._triggerEvent('failure', {
953986
data: data,
954-
src: self._globalServer
987+
src: url
955988
});
956989
}
957990
});

test/raven.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ describe('globals', function() {
10131013
maxMessageLength: 100,
10141014
release: 'abc123',
10151015
};
1016-
Raven._globalServer = 'http://localhost/store/';
1016+
Raven._globalEndpoint = 'http://localhost/store/';
10171017
Raven._globalOptions = globalOptions;
10181018

10191019
Raven._send({message: 'bar'});
@@ -1226,7 +1226,7 @@ describe('globals', function() {
12261226

12271227
it('should populate crossOrigin based on options', function() {
12281228
Raven._makeImageRequest({
1229-
url: Raven._globalServer,
1229+
url: Raven._globalEndpoint,
12301230
auth: {lol: '1'},
12311231
data: {foo: 'bar'},
12321232
options: {
@@ -1239,7 +1239,7 @@ describe('globals', function() {
12391239

12401240
it('should populate crossOrigin if empty string', function() {
12411241
Raven._makeImageRequest({
1242-
url: Raven._globalServer,
1242+
url: Raven._globalEndpoint,
12431243
auth: {lol: '1'},
12441244
data: {foo: 'bar'},
12451245
options: {
@@ -1252,7 +1252,7 @@ describe('globals', function() {
12521252

12531253
it('should not populate crossOrigin if falsey', function() {
12541254
Raven._makeImageRequest({
1255-
url: Raven._globalServer,
1255+
url: Raven._globalEndpoint,
12561256
auth: {lol: '1'},
12571257
data: {foo: 'bar'},
12581258
options: {
@@ -1487,7 +1487,7 @@ describe('Raven (public API)', function() {
14871487
Raven.afterLoad();
14881488

14891489
assert.equal(Raven._globalKey, 'random');
1490-
assert.equal(Raven._globalServer, 'http://some.other.server:80/api/2/store/');
1490+
assert.equal(Raven._globalEndpoint, 'http://some.other.server:80/api/2/store/');
14911491

14921492
assert.equal(Raven._globalOptions.some, 'config');
14931493
assert.equal(Raven._globalProject, '2');
@@ -1504,7 +1504,7 @@ describe('Raven (public API)', function() {
15041504
assert.equal(Raven, Raven.config(SENTRY_DSN, {foo: 'bar'}), 'it should return Raven');
15051505

15061506
assert.equal(Raven._globalKey, 'abc');
1507-
assert.equal(Raven._globalServer, 'http://example.com:80/api/2/store/');
1507+
assert.equal(Raven._globalEndpoint, 'http://example.com:80/api/2/store/');
15081508
assert.equal(Raven._globalOptions.foo, 'bar');
15091509
assert.equal(Raven._globalProject, '2');
15101510
assert.isTrue(Raven.isSetup());
@@ -1514,15 +1514,15 @@ describe('Raven (public API)', function() {
15141514
Raven.config('//[email protected]/2');
15151515

15161516
assert.equal(Raven._globalKey, 'abc');
1517-
assert.equal(Raven._globalServer, '//example.com/api/2/store/');
1517+
assert.equal(Raven._globalEndpoint, '//example.com/api/2/store/');
15181518
assert.equal(Raven._globalProject, '2');
15191519
assert.isTrue(Raven.isSetup());
15201520
});
15211521

15221522
it('should work should work at a non root path', function() {
15231523
Raven.config('//[email protected]/sentry/2');
15241524
assert.equal(Raven._globalKey, 'abc');
1525-
assert.equal(Raven._globalServer, '//example.com/sentry/api/2/store/');
1525+
assert.equal(Raven._globalEndpoint, '//example.com/sentry/api/2/store/');
15261526
assert.equal(Raven._globalProject, '2');
15271527
assert.isTrue(Raven.isSetup());
15281528
});

0 commit comments

Comments
 (0)