Skip to content

Commit 0d1be8a

Browse files
committed
Accept servers as base URLs instead of full API store URLs
1 parent 4fe1d31 commit 0d1be8a

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,23 @@ you want to access Sentry from.
4444
4545
Next, configure the client by passing the DSN as the first argument:
4646

47-
Raven.config('http://secret:public@example.com/project-id');
47+
Raven.config('http://public@example.com/project-id');
4848

4949
Or if you need to specify additional options:
5050

5151
Raven.config({
5252
"publicKey": "e89652ec30b94d9db6ea6f28580ab499",
53-
"servers": ["http://your.sentryserver.com/api/1/store/"],
53+
"servers": ["http://your.sentryserver.com/"],
5454
"projectId": "project-id",
5555
"logger": "yoursite.errors.javascript"
5656
});
5757

5858
**publicKey** - The desired user's public key.
5959

60-
**servers** - (*required*) An array of servers to send exception info to.
60+
**servers** - (*required*) An array of servers to send exception info to. This
61+
should be just the base URL. For example, if your API store URL is
62+
"http://mysentry.com/api/4/store/", then the base URL is "http://mysentry.com/"
63+
and the projectId is 4.
6164

6265
**projectId** - The id of the project to log the exception to. Defaults to '1'.
6366

src/raven.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
var root = this;
1313

1414
var Raven;
15-
Raven = root.Raven = {};
15+
root.Raven = Raven = {};
1616

1717
var self = Raven;
1818

@@ -37,6 +37,8 @@
3737
Raven.funcNameRE = /function\s*([\w\-$]+)?\s*\(/i;
3838

3939
Raven.config = function(config) {
40+
var servers = [];
41+
4042
if (typeof(config) === "string") {
4143
if (config.indexOf('http') === 0) {
4244
// new-style DSN configuration
@@ -45,9 +47,20 @@
4547
throw "Base64 encoded config is no longer supported - use DSN";
4648
}
4749
}
48-
$.each(config, function(i, option) {
49-
self.options[i] = option;
50+
51+
$.each(config, function(key, option) {
52+
self.options[key] = option;
53+
});
54+
55+
// Expand server base URLs into API URLs
56+
$.each(self.options['servers'], function(i, server) {
57+
// Add a trailing slash if one isn't provided
58+
if (server.slice(-1) !== '/') {
59+
server += '/';
60+
}
61+
servers.push(server + 'api/' + self.options['projectId'] + '/store/');
5062
});
63+
self.options['servers'] = servers;
5164

5265
};
5366

@@ -66,7 +79,7 @@
6679
}
6780

6881
return {
69-
servers: [uri.protocol + '://' + uri.host + ':' + uri.port + '/' + path + 'api/' + project_id + '/store/'],
82+
servers: [uri.protocol + '://' + uri.host + ':' + uri.port + '/' + path],
7083
publicKey: uri.user,
7184
secretKey: uri.password,
7285
projectId: project_id

test/process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $(document).ready(function() {
88
values[value.split('=')[0]] = value.split('=')[1];
99
});
1010
return values;
11-
};
11+
}
1212

1313
var message = "Once upon a midnight dreary",
1414
fileurl = 'http://edgarallen.poe/nevermore/',

test/test.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ <h2 id="qunit-userAgent"></h2>
4040
<ol id="qunit-tests"></ol>
4141
<div id="qunit-fixture">
4242
<!-- Reset the test environment between each test -->
43+
<script type="text/javascript" src="../src/raven.js"></script>
44+
4345
<script type="text/javascript">
4446
var config_opts = {
4547
"publicKey": "e89652ec30b94d9db6ea6f28580ab499",
4648
"secretKey": "77ec8c99a8854256aa68ccb91dd9119d",
47-
"servers": ["/api/default/store/"],
49+
"servers": ["/"],
4850
"signatureUrl": null,
4951
"testMode": true
5052
};

test/utils.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ $(document).ready(function() {
33
module("Raven.config");
44

55
test("should parse dsn as only argument", function() {
6-
// TODO: this test isnt isolated
76
var dsn = "http://public:secret@example.com:80/project-id";
87

98
Raven.config(dsn);
@@ -13,16 +12,43 @@ $(document).ready(function() {
1312
equal(config['secretKey'], 'secret');
1413
equal(config['servers'][0], 'http://example.com:80/api/project-id/store/');
1514
equal(config['projectId'], 'project-id');
15+
});
16+
17+
test("should accept servers as base URLs and add API info", function() {
18+
Raven.config({
19+
"publicKey": "public",
20+
"servers": ["http://mysentry.com/"],
21+
"projectId": "project-id"
22+
});
23+
24+
var config = Raven.options;
25+
26+
equal(config['publicKey'], 'public');
27+
equal(config['servers'][0], 'http://mysentry.com/api/project-id/store/');
28+
equal(config['projectId'], 'project-id');
29+
});
30+
31+
test("should handle base URLs without a trailing slash", function() {
32+
Raven.config({
33+
"publicKey": "public",
34+
"servers": ["http://mysentry.com"],
35+
"projectId": "project-id"
36+
});
37+
38+
var config = Raven.options;
1639

40+
equal(config['publicKey'], 'public');
41+
equal(config['servers'][0], 'http://mysentry.com/api/project-id/store/');
42+
equal(config['projectId'], 'project-id');
1743
});
1844

1945
module("Raven.parseHeaders");
2046

2147
test("should parse headers into an object", function() {
22-
var hstring = "Date: Mon, 06 Feb 2012 17:25:42 GMT\n"
23-
hstring += "Server: WSGIServer/0.1 Python/2.7.2\n"
24-
hstring += "Vary: Cookie\n"
25-
hstring += "Content-Type: text/html; charset=utf-8\n"
48+
var hstring = "Date: Mon, 06 Feb 2012 17:25:42 GMT\n";
49+
hstring += "Server: WSGIServer/0.1 Python/2.7.2\n";
50+
hstring += "Vary: Cookie\n";
51+
hstring += "Content-Type: text/html; charset=utf-8\n";
2652

2753
var headers = Raven.parseHeaders(hstring);
2854
equal(headers['Date'], "Mon, 06 Feb 2012 17:25:42 GMT");
@@ -51,7 +77,7 @@ $(document).ready(function() {
5177
var config = Raven.parseDSN(dsn);
5278
equal(config['publicKey'], 'public');
5379
equal(config['secretKey'], 'secret');
54-
equal(config['servers'][0], 'http://example.com:80/api/project-id/store/');
80+
equal(config['servers'][0], 'http://example.com:80/');
5581
equal(config['projectId'], 'project-id');
5682
});
5783

@@ -62,7 +88,7 @@ $(document).ready(function() {
6288
var config = Raven.parseDSN(dsn);
6389
equal(config['publicKey'], 'public');
6490
equal(config['secretKey'], 'secret');
65-
equal(config['servers'][0], 'http://example.com:80/path/api/project-id/store/');
91+
equal(config['servers'][0], 'http://example.com:80/path/');
6692
equal(config['projectId'], 'project-id');
6793
});
6894

@@ -72,7 +98,7 @@ $(document).ready(function() {
7298
var config = Raven.parseDSN(dsn);
7399
equal(config['publicKey'], 'public');
74100
equal(config['secretKey'], '');
75-
equal(config['servers'][0], 'http://example.com:80/path/api/project-id/store/');
101+
equal(config['servers'][0], 'http://example.com:80/path/');
76102
equal(config['projectId'], 'project-id');
77103
});
78104

0 commit comments

Comments
 (0)