Skip to content

Commit 1082ad6

Browse files
authored
feat(transports): trim down some code in the transports (#4303)
1 parent 940faf8 commit 1082ad6

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

Diff for: packages/browser/src/transports/base.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ import {
2424

2525
import { sendReport } from './utils';
2626

27-
const CATEGORY_MAPPING: {
28-
[key in SentryRequestType]: string;
29-
} = {
30-
event: 'error',
31-
transaction: 'transaction',
32-
session: 'session',
33-
attachment: 'attachment',
34-
};
27+
function requestTypeToCategory(ty: SentryRequestType): string {
28+
const tyStr = ty as string;
29+
return tyStr === 'event' ? 'error' : tyStr;
30+
}
3531

3632
const global = getGlobalObject<Window>();
3733

@@ -93,7 +89,7 @@ export abstract class BaseTransport implements Transport {
9389
// We could use nested maps, but it's much easier to read and type this way.
9490
// A correct type for map-based implementation if we want to go that route
9591
// would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>`
96-
const key = `${CATEGORY_MAPPING[category]}:${reason}`;
92+
const key = `${requestTypeToCategory(category)}:${reason}`;
9793
logger.log(`Adding outcome: ${key}`);
9894
this._outcomes[key] = (this._outcomes[key] ?? 0) + 1;
9995
}
@@ -180,7 +176,7 @@ export abstract class BaseTransport implements Transport {
180176
* Gets the time that given category is disabled until for rate limiting
181177
*/
182178
protected _disabledUntil(requestType: SentryRequestType): Date {
183-
const category = CATEGORY_MAPPING[requestType];
179+
const category = requestTypeToCategory(requestType);
184180
return this._rateLimits[category] || this._rateLimits.all;
185181
}
186182

Diff for: packages/hub/src/session.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ export class Session implements SessionInterface {
125125
errors: this.errors,
126126
did: typeof this.did === 'number' || typeof this.did === 'string' ? `${this.did}` : undefined,
127127
duration: this.duration,
128-
attrs: dropUndefinedKeys({
128+
attrs: {
129129
release: this.release,
130130
environment: this.environment,
131131
ip_address: this.ipAddress,
132132
user_agent: this.userAgent,
133-
}),
133+
},
134134
});
135135
}
136136
}

Diff for: packages/utils/src/dsn.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DsnComponents, DsnLike, DsnProtocol } from '@sentry/types';
22

3+
import { isDebugBuild } from './env';
34
import { SentryError } from './error';
45

56
/** Regular expression used to parse a Dsn. */
@@ -102,22 +103,25 @@ export class Dsn implements DsnComponents {
102103

103104
/** Validates this Dsn and throws on error. */
104105
private _validate(): void {
105-
['protocol', 'publicKey', 'host', 'projectId'].forEach(component => {
106-
if (!this[component as keyof DsnComponents]) {
107-
throw new SentryError(`${ERROR_MESSAGE}: ${component} missing`);
106+
// we only validate in debug mode. This will fail later anyways.
107+
if (isDebugBuild()) {
108+
['protocol', 'publicKey', 'host', 'projectId'].forEach(component => {
109+
if (!this[component as keyof DsnComponents]) {
110+
throw new SentryError(`${ERROR_MESSAGE}: ${component} missing`);
111+
}
112+
});
113+
114+
if (!this.projectId.match(/^\d+$/)) {
115+
throw new SentryError(`${ERROR_MESSAGE}: Invalid projectId ${this.projectId}`);
108116
}
109-
});
110117

111-
if (!this.projectId.match(/^\d+$/)) {
112-
throw new SentryError(`${ERROR_MESSAGE}: Invalid projectId ${this.projectId}`);
113-
}
114-
115-
if (this.protocol !== 'http' && this.protocol !== 'https') {
116-
throw new SentryError(`${ERROR_MESSAGE}: Invalid protocol ${this.protocol}`);
117-
}
118+
if (this.protocol !== 'http' && this.protocol !== 'https') {
119+
throw new SentryError(`${ERROR_MESSAGE}: Invalid protocol ${this.protocol}`);
120+
}
118121

119-
if (this.port && isNaN(parseInt(this.port, 10))) {
120-
throw new SentryError(`${ERROR_MESSAGE}: Invalid port ${this.port}`);
122+
if (this.port && isNaN(parseInt(this.port, 10))) {
123+
throw new SentryError(`${ERROR_MESSAGE}: Invalid port ${this.port}`);
124+
}
121125
}
122126
}
123127
}

Diff for: packages/utils/test/dsn.test.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { isDebugBuild } from '@sentry/utils';
2+
13
import { Dsn } from '../src/dsn';
24
import { SentryError } from '../src/error';
35

6+
function testIf(condition: boolean): jest.It {
7+
return condition ? test : test.skip;
8+
}
9+
410
describe('Dsn', () => {
511
describe('fromComponents', () => {
612
test('applies all components', () => {
@@ -37,7 +43,7 @@ describe('Dsn', () => {
3743
expect(dsn.projectId).toBe('123');
3844
});
3945

40-
test('throws for missing components', () => {
46+
testIf(isDebugBuild())('throws for missing components', () => {
4147
expect(
4248
() =>
4349
new Dsn({
@@ -76,7 +82,7 @@ describe('Dsn', () => {
7682
).toThrow(SentryError);
7783
});
7884

79-
test('throws for invalid components', () => {
85+
testIf(isDebugBuild())('throws for invalid components', () => {
8086
expect(
8187
() =>
8288
new Dsn({
@@ -144,18 +150,18 @@ describe('Dsn', () => {
144150
expect(dsn.projectId).toBe('321');
145151
});
146152

147-
test('throws when provided invalid Dsn', () => {
153+
testIf(isDebugBuild())('throws when provided invalid Dsn', () => {
148154
expect(() => new Dsn('[email protected]')).toThrow(SentryError);
149155
});
150156

151-
test('throws without mandatory fields', () => {
157+
testIf(isDebugBuild())('throws without mandatory fields', () => {
152158
expect(() => new Dsn('://[email protected]/123')).toThrow(SentryError);
153159
expect(() => new Dsn('https://@sentry.io/123')).toThrow(SentryError);
154160
expect(() => new Dsn('https://abc@123')).toThrow(SentryError);
155161
expect(() => new Dsn('https://[email protected]/')).toThrow(SentryError);
156162
});
157163

158-
test('throws for invalid fields', () => {
164+
testIf(isDebugBuild())('throws for invalid fields', () => {
159165
expect(() => new Dsn('httpx://[email protected]/123')).toThrow(SentryError);
160166
expect(() => new Dsn('httpx://[email protected]:xxx/123')).toThrow(SentryError);
161167
expect(() => new Dsn('http://[email protected]/abc')).toThrow(SentryError);

0 commit comments

Comments
 (0)