Skip to content

Commit c83c1c5

Browse files
committed
ref: Pass Event to sendEvent instead of just a body
1 parent 60b6e75 commit c83c1c5

File tree

18 files changed

+69
-91
lines changed

18 files changed

+69
-91
lines changed

packages/browser/src/transports/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { API } from '@sentry/core';
2-
import { Response, Transport, TransportOptions } from '@sentry/types';
2+
import { Event, Response, Transport, TransportOptions } from '@sentry/types';
33
import { SentryError } from '@sentry/utils/error';
44
import { PromiseBuffer } from '@sentry/utils/promisebuffer';
55

@@ -20,7 +20,7 @@ export abstract class BaseTransport implements Transport {
2020
/**
2121
* @inheritDoc
2222
*/
23-
public async sendEvent(_: string): Promise<Response> {
23+
public async sendEvent(_: Event): Promise<Response> {
2424
throw new SentryError('Transport Class has to implement `sendEvent` method');
2525
}
2626

packages/browser/src/transports/beacon.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Response, Status } from '@sentry/types';
1+
import { Event, Response, Status } from '@sentry/types';
22
import { getGlobalObject } from '@sentry/utils/misc';
33
import { BaseTransport } from './base';
44

@@ -9,8 +9,8 @@ export class BeaconTransport extends BaseTransport {
99
/**
1010
* @inheritDoc
1111
*/
12-
public async sendEvent(body: string): Promise<Response> {
13-
const result = global.navigator.sendBeacon(this.url, body);
12+
public async sendEvent(event: Event): Promise<Response> {
13+
const result = global.navigator.sendBeacon(this.url, JSON.stringify(event));
1414

1515
return this.buffer.add(
1616
Promise.resolve({

packages/browser/src/transports/fetch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Response, Status } from '@sentry/types';
1+
import { Event, Response, Status } from '@sentry/types';
22
import { getGlobalObject } from '@sentry/utils/misc';
33
import { supportsReferrerPolicy } from '@sentry/utils/supports';
44
import { BaseTransport } from './base';
@@ -10,9 +10,9 @@ export class FetchTransport extends BaseTransport {
1010
/**
1111
* @inheritDoc
1212
*/
13-
public async sendEvent(body: string): Promise<Response> {
13+
public async sendEvent(event: Event): Promise<Response> {
1414
const defaultOptions: RequestInit = {
15-
body,
15+
body: JSON.stringify(event),
1616
method: 'POST',
1717
// Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default
1818
// https://caniuse.com/#feat=referrer-policy

packages/browser/src/transports/xhr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Response, Status } from '@sentry/types';
1+
import { Event, Response, Status } from '@sentry/types';
22
import { BaseTransport } from './base';
33

44
/** `XHR` based transport */
55
export class XHRTransport extends BaseTransport {
66
/**
77
* @inheritDoc
88
*/
9-
public async sendEvent(body: string): Promise<Response> {
9+
public async sendEvent(event: Event): Promise<Response> {
1010
return this.buffer.add(
1111
new Promise<Response>((resolve, reject) => {
1212
const request = new XMLHttpRequest();
@@ -26,7 +26,7 @@ export class XHRTransport extends BaseTransport {
2626
};
2727

2828
request.open('POST', this.url);
29-
request.send(body);
29+
request.send(JSON.stringify(event));
3030
}),
3131
);
3232
}

packages/browser/test/integration/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function initSDK() {
107107
// stub transport so we don't actually transmit any data
108108
function DummyTransport() {}
109109
DummyTransport.prototype.sendEvent = function(event) {
110-
sentryData.push(JSON.parse(event));
110+
sentryData.push(event);
111111
done(sentryData);
112112
return Promise.resolve({
113113
status: 'success',

packages/browser/test/mocks/simpletransport.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Response, Status } from '../../src';
1+
import { Event, Response, Status } from '../../src';
22
import { BaseTransport } from '../../src/transports';
33

44
export class SimpleTransport extends BaseTransport {
5-
public async sendEvent(_: string): Promise<Response> {
5+
public async sendEvent(_: Event): Promise<Response> {
66
return this.buffer.add(
77
Promise.resolve({
88
status: Status.fromHttpCode(200),

packages/browser/test/transports/base.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('BaseTransport', () => {
1010
const transport = new SimpleTransport({ dsn: testDsn });
1111

1212
try {
13-
await transport.sendEvent('');
13+
await transport.sendEvent({});
1414
} catch (e) {
1515
expect(e.message).equal('Transport Class has to implement `sendEvent` method');
1616
}

packages/browser/test/transports/beacon.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('BeaconTransport', () => {
3333
it('sends a request to Sentry servers', async () => {
3434
sendBeacon.returns(true);
3535

36-
return transport.sendEvent(JSON.stringify(payload)).then(res => {
36+
return transport.sendEvent(payload).then(res => {
3737
expect(res.status).equal(Status.Success);
3838
expect(sendBeacon.calledOnce).equal(true);
3939
expect(sendBeacon.calledWith(transportUrl, JSON.stringify(payload))).equal(true);
@@ -43,7 +43,7 @@ describe('BeaconTransport', () => {
4343
it('rejects with failed status', async () => {
4444
sendBeacon.returns(false);
4545

46-
return transport.sendEvent(JSON.stringify(payload)).catch(res => {
46+
return transport.sendEvent(payload).catch(res => {
4747
expect(res.status).equal(Status.Failed);
4848
expect(sendBeacon.calledOnce).equal(true);
4949
expect(sendBeacon.calledWith(transportUrl, JSON.stringify(payload))).equal(true);

packages/browser/test/transports/fetch.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('FetchTransport', () => {
3535

3636
fetch.returns(Promise.resolve(response));
3737

38-
return transport.sendEvent(JSON.stringify(payload)).then(res => {
38+
return transport.sendEvent(payload).then(res => {
3939
expect(res.status).equal(Status.Success);
4040
expect(fetch.calledOnce).equal(true);
4141
expect(
@@ -53,7 +53,7 @@ describe('FetchTransport', () => {
5353

5454
fetch.returns(Promise.reject(response));
5555

56-
return transport.sendEvent(JSON.stringify(payload)).catch(res => {
56+
return transport.sendEvent(payload).catch(res => {
5757
expect(res.status).equal(403);
5858
expect(fetch.calledOnce).equal(true);
5959
expect(

packages/browser/test/transports/xhr.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('XHRTransport', () => {
3434
it('sends a request to Sentry servers', async () => {
3535
server.respondWith('POST', transportUrl, [200, {}, '']);
3636

37-
return transport.sendEvent(JSON.stringify(payload)).then(res => {
37+
return transport.sendEvent(payload).then(res => {
3838
expect(res.status).equal(Status.Success);
3939
const request = server.requests[0];
4040
expect(server.requests.length).equal(1);
@@ -46,7 +46,7 @@ describe('XHRTransport', () => {
4646
it('rejects with non-200 status code', done => {
4747
server.respondWith('POST', transportUrl, [403, {}, '']);
4848

49-
transport.sendEvent(JSON.stringify(payload)).catch(res => {
49+
transport.sendEvent(payload).catch(res => {
5050
expect(res.status).equal(403);
5151

5252
const request = server.requests[0];

packages/core/src/basebackend.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Event, EventHint, Options, Severity, Transport } from '@sentry/types';
22
import { SentryError } from '@sentry/utils/error';
33
import { logger } from '@sentry/utils/logger';
4-
import { serialize } from '@sentry/utils/object';
54
import { SyncPromise } from '@sentry/utils/syncpromise';
65
import { NoopTransport } from './transports/noop';
76

@@ -95,7 +94,7 @@ export abstract class BaseBackend<O extends Options> implements Backend {
9594
* @inheritDoc
9695
*/
9796
public sendEvent(event: Event): void {
98-
this.transport.sendEvent(serialize(event)).catch(reason => {
97+
this.transport.sendEvent(event).catch(reason => {
9998
logger.error(`Error while sending event: ${reason}`);
10099
});
101100
}

packages/core/src/transports/noop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Response, Status, Transport } from '@sentry/types';
1+
import { Event, Response, Status, Transport } from '@sentry/types';
22

33
/** Noop transport */
44
export class NoopTransport implements Transport {
55
/**
66
* @inheritDoc
77
*/
8-
public async sendEvent(_: string): Promise<Response> {
8+
public async sendEvent(_: Event): Promise<Response> {
99
return Promise.resolve({
1010
reason: `NoopTransport: Event has been skipped because no Dsn is configured.`,
1111
status: Status.Skipped,

packages/node/src/transports/base.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { API } from '@sentry/core';
2-
import { Response, Status, Transport, TransportOptions } from '@sentry/types';
2+
import { Event, Response, Status, Transport, TransportOptions } from '@sentry/types';
33
import { SentryError } from '@sentry/utils/error';
44
import { PromiseBuffer } from '@sentry/utils/promisebuffer';
55
import * as fs from 'fs';
@@ -71,7 +71,7 @@ export abstract class BaseTransport implements Transport {
7171
}
7272

7373
/** JSDoc */
74-
protected async sendWithModule(httpModule: HTTPRequest, body: string): Promise<Response> {
74+
protected async sendWithModule(httpModule: HTTPRequest, event: Event): Promise<Response> {
7575
return this.buffer.add(
7676
new Promise<Response>((resolve, reject) => {
7777
const req = httpModule.request(this.getRequestOptions(), (res: http.IncomingMessage) => {
@@ -97,15 +97,15 @@ export abstract class BaseTransport implements Transport {
9797
});
9898
});
9999
req.on('error', reject);
100-
req.end(body);
100+
req.end(JSON.stringify(event));
101101
}),
102102
);
103103
}
104104

105105
/**
106106
* @inheritDoc
107107
*/
108-
public async sendEvent(_: string): Promise<Response> {
108+
public async sendEvent(_: Event): Promise<Response> {
109109
throw new SentryError('Transport Class has to implement `sendEvent` method.');
110110
}
111111

packages/node/src/transports/http.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Response, TransportOptions } from '@sentry/types';
1+
import { Event, Response, TransportOptions } from '@sentry/types';
22
import { SentryError } from '@sentry/utils/error';
33
import * as http from 'http';
44
import * as HttpsProxyAgent from 'https-proxy-agent';
@@ -20,10 +20,10 @@ export class HTTPTransport extends BaseTransport {
2020
/**
2121
* @inheritDoc
2222
*/
23-
public async sendEvent(body: string): Promise<Response> {
23+
public async sendEvent(event: Event): Promise<Response> {
2424
if (!this.module) {
2525
throw new SentryError('No module available in HTTPTransport');
2626
}
27-
return this.sendWithModule(this.module, body);
27+
return this.sendWithModule(this.module, event);
2828
}
2929
}

packages/node/src/transports/https.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Response, TransportOptions } from '@sentry/types';
1+
import { Event, Response, TransportOptions } from '@sentry/types';
22
import { SentryError } from '@sentry/utils/error';
33
import * as https from 'https';
44
import * as HttpsProxyAgent from 'https-proxy-agent';
@@ -20,10 +20,10 @@ export class HTTPSTransport extends BaseTransport {
2020
/**
2121
* @inheritDoc
2222
*/
23-
public async sendEvent(body: string): Promise<Response> {
23+
public async sendEvent(event: Event): Promise<Response> {
2424
if (!this.module) {
2525
throw new SentryError('No module available in HTTPSTransport');
2626
}
27-
return this.sendWithModule(this.module, body);
27+
return this.sendWithModule(this.module, event);
2828
}
2929
}

packages/node/test/transports/http.test.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ describe('HTTPTransport', () => {
4444

4545
test('send 200', async () => {
4646
const transport = createTransport({ dsn });
47-
await transport.sendEvent(
48-
JSON.stringify({
49-
message: 'test',
50-
}),
51-
);
47+
await transport.sendEvent({
48+
message: 'test',
49+
});
5250

5351
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
5452
assertBasicOptions(requestOptions);
@@ -60,11 +58,9 @@ describe('HTTPTransport', () => {
6058
const transport = createTransport({ dsn });
6159

6260
try {
63-
await transport.sendEvent(
64-
JSON.stringify({
65-
message: 'test',
66-
}),
67-
);
61+
await transport.sendEvent({
62+
message: 'test',
63+
});
6864
} catch (e) {
6965
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
7066
assertBasicOptions(requestOptions);
@@ -80,11 +76,9 @@ describe('HTTPTransport', () => {
8076
const transport = createTransport({ dsn });
8177

8278
try {
83-
await transport.sendEvent(
84-
JSON.stringify({
85-
message: 'test',
86-
}),
87-
);
79+
await transport.sendEvent({
80+
message: 'test',
81+
});
8882
} catch (e) {
8983
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
9084
assertBasicOptions(requestOptions);
@@ -100,11 +94,9 @@ describe('HTTPTransport', () => {
10094
a: 'b',
10195
},
10296
});
103-
await transport.sendEvent(
104-
JSON.stringify({
105-
message: 'test',
106-
}),
107-
);
97+
await transport.sendEvent({
98+
message: 'test',
99+
});
108100

109101
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
110102
assertBasicOptions(requestOptions);
@@ -117,11 +109,9 @@ describe('HTTPTransport', () => {
117109
dsn,
118110
httpProxy: 'http://example.com:8080',
119111
});
120-
await transport.sendEvent(
121-
JSON.stringify({
122-
message: 'test',
123-
}),
124-
);
112+
await transport.sendEvent({
113+
message: 'test',
114+
});
125115

126116
const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0];
127117
assertBasicOptions(requestOptions);

0 commit comments

Comments
 (0)