Skip to content

ref: Loosen promise typings #2273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"rollup": "^1.10.1",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-license": "^0.8.1",
"rollup-plugin-modify": "^3.0.0",
"rollup-plugin-node-resolve": "^4.2.3",
"rollup-plugin-terser": "^4.0.4",
"rollup-plugin-typescript2": "^0.21.0",
Expand Down
6 changes: 0 additions & 6 deletions packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import typescript from 'rollup-plugin-typescript2';
import license from 'rollup-plugin-license';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import modify from 'rollup-plugin-modify';

const commitHash = require('child_process')
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
Expand Down Expand Up @@ -47,11 +46,6 @@ const plugins = [
mainFields: ['module'],
}),
commonjs(),
modify({
// It's very difficult to use Symbol without polyfilling in IE10 and still making TypeScript behave correctly.
// Just remove it and leave this space in there, so that SourceMaps are still correct.
"this[Symbol.toStringTag] = '[object SyncPromise]';": ' ',
}),
];

const bundleConfig = {
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
/**
* @inheritDoc
*/
public eventFromException(exception: any, hint?: EventHint): Promise<Event> {
public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromUnknownInput(exception, syntheticException, {
attachStacktrace: this._options.attachStacktrace,
Expand All @@ -74,7 +74,7 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
/**
* @inheritDoc
*/
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): Promise<Event> {
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromString(message, syntheticException, {
attachStacktrace: this._options.attachStacktrace,
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
/**
* @inheritDoc
*/
protected _prepareEvent(event: Event, scope?: Scope, hint?: EventHint): Promise<Event | null> {
protected _prepareEvent(event: Event, scope?: Scope, hint?: EventHint): PromiseLike<Event | null> {
event.platform = event.platform || 'javascript';
event.sdk = {
...event.sdk,
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class Breadcrumbs implements Integration {
);
return response;
})
.catch((error: Error) => {
.then(null, (error: Error) => {
Breadcrumbs.addBreadcrumb(
{
category: 'fetch',
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function onLoad(callback: () => void): void {
*
* @param timeout Maximum time in ms the client should wait.
*/
export function flush(timeout?: number): Promise<boolean> {
export function flush(timeout?: number): PromiseLike<boolean> {
const client = getCurrentHub().getClient<BrowserClient>();
if (client) {
return client.flush(timeout);
Expand All @@ -147,7 +147,7 @@ export function flush(timeout?: number): Promise<boolean> {
*
* @param timeout Maximum time in ms the client should wait.
*/
export function close(timeout?: number): Promise<boolean> {
export function close(timeout?: number): PromiseLike<boolean> {
const client = getCurrentHub().getClient<BrowserClient>();
if (client) {
return client.close(timeout);
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export abstract class BaseTransport implements Transport {
/**
* @inheritDoc
*/
public sendEvent(_: Event): Promise<Response> {
public sendEvent(_: Event): PromiseLike<Response> {
throw new SentryError('Transport Class has to implement `sendEvent` method');
}

/**
* @inheritDoc
*/
public close(timeout?: number): Promise<boolean> {
public close(timeout?: number): PromiseLike<boolean> {
return this._buffer.drain(timeout);
}
}
2 changes: 1 addition & 1 deletion packages/browser/src/transports/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FetchTransport extends BaseTransport {
/**
* @inheritDoc
*/
public sendEvent(event: Event): Promise<Response> {
public sendEvent(event: Event): PromiseLike<Response> {
const defaultOptions: RequestInit = {
body: JSON.stringify(event),
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/transports/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class XHRTransport extends BaseTransport {
/**
* @inheritDoc
*/
public sendEvent(event: Event): Promise<Response> {
public sendEvent(event: Event): PromiseLike<Response> {
return this._buffer.add(
new SyncPromise<Response>((resolve, reject) => {
const request = new XMLHttpRequest();
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/unit/mocks/simpletransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Event, Response, Status } from '../../../src';
import { BaseTransport } from '../../../src/transports';

export class SimpleTransport extends BaseTransport {
public sendEvent(_: Event): Promise<Response> {
public sendEvent(_: Event): PromiseLike<Response> {
return this._buffer.add(
SyncPromise.resolve({
status: Status.fromHttpCode(200),
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/unit/transports/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('FetchTransport', () => {

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

return transport.sendEvent(payload).catch(res => {
return transport.sendEvent(payload).then(null, res => {
expect(res.status).equal(403);
expect(fetch.calledOnce).equal(true);
expect(
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/unit/transports/xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('XHRTransport', () => {
it('rejects with non-200 status code', done => {
server.respondWith('POST', transportUrl, [403, {}, '']);

transport.sendEvent(payload).catch(res => {
transport.sendEvent(payload).then(null, res => {
expect(res.status).equal(403);

const request = server.requests[0];
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/basebackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import { NoopTransport } from './transports/noop';
*/
export interface Backend {
/** Creates a {@link Event} from an exception. */
eventFromException(exception: any, hint?: EventHint): Promise<Event>;
eventFromException(exception: any, hint?: EventHint): PromiseLike<Event>;

/** Creates a {@link Event} from a plain message. */
eventFromMessage(message: string, level?: Severity, hint?: EventHint): Promise<Event>;
eventFromMessage(message: string, level?: Severity, hint?: EventHint): PromiseLike<Event>;

/** Submits the event to Sentry */
sendEvent(event: Event): void;
Expand Down Expand Up @@ -78,22 +78,22 @@ export abstract class BaseBackend<O extends Options> implements Backend {
/**
* @inheritDoc
*/
public eventFromException(_exception: any, _hint?: EventHint): Promise<Event> {
public eventFromException(_exception: any, _hint?: EventHint): PromiseLike<Event> {
throw new SentryError('Backend has to implement `eventFromException` method');
}

/**
* @inheritDoc
*/
public eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): Promise<Event> {
public eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): PromiseLike<Event> {
throw new SentryError('Backend has to implement `eventFromMessage` method');
}

/**
* @inheritDoc
*/
public sendEvent(event: Event): void {
this._transport.sendEvent(event).catch(reason => {
this._transport.sendEvent(event).then(null, reason => {
logger.error(`Error while sending event: ${reason}`);
});
}
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
eventId = finalEvent && finalEvent.event_id;
this._processing = false;
})
.catch(reason => {
.then(null, reason => {
logger.error(reason);
this._processing = false;
});
Expand All @@ -119,7 +119,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
eventId = finalEvent && finalEvent.event_id;
this._processing = false;
})
.catch(reason => {
.then(null, reason => {
logger.error(reason);
this._processing = false;
});
Expand All @@ -140,7 +140,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
eventId = finalEvent && finalEvent.event_id;
this._processing = false;
})
.catch(reason => {
.then(null, reason => {
logger.error(reason);
this._processing = false;
});
Expand All @@ -164,7 +164,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
/**
* @inheritDoc
*/
public flush(timeout?: number): Promise<boolean> {
public flush(timeout?: number): PromiseLike<boolean> {
return this._isClientProcessing(timeout).then(status => {
clearInterval(status.interval);
return this._getBackend()
Expand All @@ -177,7 +177,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
/**
* @inheritDoc
*/
public close(timeout?: number): Promise<boolean> {
public close(timeout?: number): PromiseLike<boolean> {
return this.flush(timeout).then(result => {
this.getOptions().enabled = false;
return result;
Expand All @@ -204,7 +204,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
}

/** Waits for the client to be done with processing. */
protected _isClientProcessing(timeout?: number): Promise<{ ready: boolean; interval: number }> {
protected _isClientProcessing(timeout?: number): PromiseLike<{ ready: boolean; interval: number }> {
return new SyncPromise<{ ready: boolean; interval: number }>(resolve => {
let ticked: number = 0;
const tick: number = 1;
Expand Down Expand Up @@ -255,7 +255,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
* @param scope A scope containing event metadata.
* @returns A new event with more information.
*/
protected _prepareEvent(event: Event, scope?: Scope, hint?: EventHint): Promise<Event | null> {
protected _prepareEvent(event: Event, scope?: Scope, hint?: EventHint): PromiseLike<Event | null> {
const { environment, release, dist, maxValueLength = 250 } = this.getOptions();

const prepared: Event = { ...event };
Expand Down Expand Up @@ -327,7 +327,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
* @param scope A scope containing event metadata.
* @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send.
*/
protected _processEvent(event: Event, hint?: EventHint, scope?: Scope): Promise<Event> {
protected _processEvent(event: Event, hint?: EventHint, scope?: Scope): PromiseLike<Event> {
const { beforeSend, sampleRate } = this.getOptions();

if (!this._isEnabled()) {
Expand Down Expand Up @@ -362,7 +362,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
if ((typeof beforeSendResult as any) === 'undefined') {
logger.error('`beforeSend` method has to return `null` or a valid event.');
} else if (isThenable(beforeSendResult)) {
this._handleAsyncBeforeSend(beforeSendResult as Promise<Event | null>, resolve, reject);
this._handleAsyncBeforeSend(beforeSendResult as PromiseLike<Event | null>, resolve, reject);
} else {
finalEvent = beforeSendResult as Event | null;

Expand All @@ -386,7 +386,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
reject('`beforeSend` threw an error, will not send event.');
}
})
.catch(() => {
.then(null, () => {
reject('`beforeSend` threw an error, will not send event.');
});
});
Expand All @@ -396,7 +396,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
* Resolves before send Promise and calls resolve/reject on parent SyncPromise.
*/
private _handleAsyncBeforeSend(
beforeSend: Promise<Event | null>,
beforeSend: PromiseLike<Event | null>,
resolve: (event: Event) => void,
reject: (reason: string) => void,
): void {
Expand All @@ -410,7 +410,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
this._getBackend().sendEvent(processedEvent);
resolve(processedEvent);
})
.catch(e => {
.then(null, e => {
reject(`beforeSend rejected with ${e}`);
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/transports/noop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class NoopTransport implements Transport {
/**
* @inheritDoc
*/
public sendEvent(_: Event): Promise<Response> {
public sendEvent(_: Event): PromiseLike<Response> {
return SyncPromise.resolve({
reason: `NoopTransport: Event has been skipped because no Dsn is configured.`,
status: Status.Skipped,
Expand All @@ -16,7 +16,7 @@ export class NoopTransport implements Transport {
/**
* @inheritDoc
*/
public close(_?: number): Promise<boolean> {
public close(_?: number): PromiseLike<boolean> {
return SyncPromise.resolve(true);
}
}
4 changes: 2 additions & 2 deletions packages/core/test/mocks/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class TestBackend extends BaseBackend<TestOptions> {
return super._setupTransport();
}

public eventFromException(exception: any): Promise<Event> {
public eventFromException(exception: any): PromiseLike<Event> {
return SyncPromise.resolve({
exception: {
values: [
Expand All @@ -50,7 +50,7 @@ export class TestBackend extends BaseBackend<TestOptions> {
});
}

public eventFromMessage(message: string): Promise<Event> {
public eventFromMessage(message: string): PromiseLike<Event> {
return SyncPromise.resolve({ message });
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/mocks/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class FakeTransport implements Transport {
public sentCount: number = 0;
public delay: number = 2000;

public sendEvent(_event: Event): Promise<Response> {
public sendEvent(_event: Event): PromiseLike<Response> {
this.sendCalled += 1;
return this._buffer.add(
new SyncPromise(async res => {
Expand All @@ -24,7 +24,7 @@ export class FakeTransport implements Transport {
);
}

public close(timeout?: number): Promise<boolean> {
public close(timeout?: number): PromiseLike<boolean> {
return this._buffer.drain(timeout);
}
}
10 changes: 5 additions & 5 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Scope implements ScopeInterface {
event: Event | null,
hint?: EventHint,
index: number = 0,
): Promise<Event | null> {
): PromiseLike<Event | null> {
return new SyncPromise<Event | null>((resolve, reject) => {
const processor = processors[index];
// tslint:disable-next-line:strict-type-predicates
Expand All @@ -92,13 +92,13 @@ export class Scope implements ScopeInterface {
} else {
const result = processor({ ...event }, hint) as Event | null;
if (isThenable(result)) {
(result as Promise<Event | null>)
(result as PromiseLike<Event | null>)
.then(final => this._notifyEventProcessors(processors, final, hint, index + 1).then(resolve))
.catch(reject);
.then(null, reject);
} else {
this._notifyEventProcessors(processors, result, hint, index + 1)
.then(resolve)
.catch(reject);
.then(null, reject);
}
}
});
Expand Down Expand Up @@ -311,7 +311,7 @@ export class Scope implements ScopeInterface {
* @param hint May contain additional informartion about the original exception.
* @hidden
*/
public applyToEvent(event: Event, hint?: EventHint): Promise<Event | null> {
public applyToEvent(event: Event, hint?: EventHint): PromiseLike<Event | null> {
if (this._extra && Object.keys(this._extra).length) {
event.extra = { ...this._extra, ...event.extra };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hub/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('Hub', () => {
expect(appliedEvent!.breadcrumbs![1].message).toEqual('scope breadcrumb');
expect(appliedEvent!.breadcrumbs![1]).toHaveProperty('timestamp');
})
.catch(e => {
.then(null, e => {
console.error(e);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/hub/test/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ describe('Scope', () => {
return processedEvent;
});

return localScope.applyToEvent(event).catch(reason => {
return localScope.applyToEvent(event).then(null, reason => {
expect(reason).toEqual('bla');
});
});
Expand Down
Loading