Skip to content

Commit

Permalink
fix: Handle DSN qs and show better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jun 3, 2020
1 parent c82f359 commit f86bfc7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
17 changes: 14 additions & 3 deletions packages/utils/src/dsn.ts
Expand Up @@ -72,6 +72,13 @@ export class Dsn implements DsnComponents {
projectId = split.pop() as string;
}

if (projectId) {
const projectMatch = projectId.match(/^\d+/);
if (projectMatch) {
projectId = projectMatch[0];
}
}

this._fromComponents({ host, pass, path, projectId, port, protocol: protocol as DsnProtocol, user });
}

Expand All @@ -90,16 +97,20 @@ export class Dsn implements DsnComponents {
private _validate(): void {
['protocol', 'user', 'host', 'projectId'].forEach(component => {
if (!this[component as keyof DsnComponents]) {
throw new SentryError(ERROR_MESSAGE);
throw new SentryError(`${ERROR_MESSAGE}: ${component} missing`);
}
});

if (!this.projectId.match(/^\d+$/)) {
throw new SentryError(`${ERROR_MESSAGE}: Invalid projectId ${this.projectId}`);
}

if (this.protocol !== 'http' && this.protocol !== 'https') {
throw new SentryError(ERROR_MESSAGE);
throw new SentryError(`${ERROR_MESSAGE}: Invalid protocol ${this.protocol}`);
}

if (this.port && isNaN(parseInt(this.port, 10))) {
throw new SentryError(ERROR_MESSAGE);
throw new SentryError(`${ERROR_MESSAGE}: Invalid port ${this.port}`);
}
}
}
18 changes: 15 additions & 3 deletions packages/utils/test/dsn.test.ts
Expand Up @@ -17,8 +17,8 @@ describe('Dsn', () => {
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('123');
});

test('applies partial components', () => {
Expand All @@ -33,8 +33,8 @@ describe('Dsn', () => {
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('123');
});

test('throws for missing components', () => {
Expand Down Expand Up @@ -107,8 +107,8 @@ describe('Dsn', () => {
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('123');
});

test('parses a valid partial Dsn', () => {
Expand All @@ -133,6 +133,17 @@ describe('Dsn', () => {
expect(dsn.projectId).toBe('321');
});

test('with a query string', () => {
const dsn = new Dsn('https://abc@sentry.io/321?sample.rate=0.1&other=value');
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('321');
});

test('throws when provided invalid Dsn', () => {
expect(() => new Dsn('some@random.dsn')).toThrow(SentryError);
});
Expand All @@ -147,6 +158,7 @@ describe('Dsn', () => {
test('throws for invalid fields', () => {
expect(() => new Dsn('httpx://abc@sentry.io/123')).toThrow(SentryError);
expect(() => new Dsn('httpx://abc@sentry.io:xxx/123')).toThrow(SentryError);
expect(() => new Dsn('http://abc@sentry.io/abc')).toThrow(SentryError);
});
});

Expand Down

0 comments on commit f86bfc7

Please sign in to comment.