Skip to content
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

Enable eslint rule and reject with errors instead of strings #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default [
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/only-throw-error': 'off',
'@typescript-eslint/prefer-promise-reject-errors': 'off',
'@typescript-eslint/prefer-promise-reject-errors': 'error',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/restrict-template-expressions': 'error',
'@typescript-eslint/unbound-method': 'off',
Expand Down
2 changes: 1 addition & 1 deletion examples/rpc-transport-throttled/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function getThrottledTransport<TClusterUrl extends ClusterUrl>(
} as QueuedRequest<TClusterUrl>);
if (config.signal) {
config.signal.addEventListener('abort', function () {
reject(this.reason);
reject(new Error(String(this.reason)));
});
}
processQueue();
Expand Down
2 changes: 1 addition & 1 deletion packages/promises/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This package contains helpers for using JavaScript promises.

### `getAbortablePromise(promise, abortSignal?)`

Rejects if the `abortSignal` is aborted before the promise settles. Resolves or rejects with the value of the promise otherwise.
Rejects if the `abortSignal` is aborted before the promise settles. Resolves with the promise's value or rejects with an Error containing the rejection reason.

```ts
const result = await getAbortablePromise(
Expand Down
8 changes: 4 additions & 4 deletions packages/promises/src/__tests__/abortable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ describe('getAbortablePromise()', () => {
it('rejects with the `reason` when passed an already-aborted signal with a pending promise', async () => {
expect.assertions(1);
const signal = AbortSignal.abort('o no');
await expect(getAbortablePromise(promise, signal)).rejects.toBe('o no');
await expect(getAbortablePromise(promise, signal)).rejects.toThrow('o no');
});
it('rejects with the `reason` when passed an already-aborted signal and an already-resolved promise', async () => {
expect.assertions(1);
const signal = AbortSignal.abort('o no');
resolve(123);
await expect(getAbortablePromise(promise, signal)).rejects.toBe('o no');
await expect(getAbortablePromise(promise, signal)).rejects.toThrow('o no');
});
it('rejects with the `reason` when passed an already-aborted signal and an already-rejected promise', async () => {
expect.assertions(1);
const signal = AbortSignal.abort('o no');
reject('mais non');
await expect(getAbortablePromise(promise, signal)).rejects.toBe('o no');
await expect(getAbortablePromise(promise, signal)).rejects.toThrow('o no');
});
it('rejects with the `reason` when the signal aborts before the promise settles', async () => {
expect.assertions(2);
const controller = new AbortController();
const abortablePromise = getAbortablePromise(promise, controller.signal);
await expect(Promise.race(['pending', abortablePromise])).resolves.toBe('pending');
controller.abort('o no');
await expect(abortablePromise).rejects.toBe('o no');
await expect(abortablePromise).rejects.toThrow('o no');
});
it('rejects with the promise rejection when passed an already-rejected promise and a not-yet-aborted signal', async () => {
expect.assertions(1);
Expand Down
7 changes: 3 additions & 4 deletions packages/promises/src/abortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ export function getAbortablePromise<T>(promise: Promise<T>, abortSignal?: AbortS
// It's important that this come before the input promise; in the event of an abort, we
// want to throw even if the input promise's result is ready
new Promise<never>((_, reject) => {
const handleReject = () => reject(new Error(String(abortSignal.reason)));
if (abortSignal.aborted) {
reject(abortSignal.reason);
handleReject();
} else {
abortSignal.addEventListener('abort', function () {
reject(this.reason);
});
abortSignal.addEventListener('abort', handleReject);
}
}),
promise,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createWebSocketChannel({
url,
}: Config): Promise<RpcSubscriptionsChannel<WebSocketMessage, string>> {
if (signal.aborted) {
return Promise.reject(signal.reason);
return Promise.reject(new Error(String(signal.reason)));
}
let bufferDrainWatcher: Readonly<{ onCancel(): void; promise: Promise<void> }> | undefined;
let hasConnected = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function executeRpcPubSubSubscriptionPlan<TNotification>({
subscriptionId = undefined;
channel.send(unsubscribePayload).catch(() => {});
}
reject(this.reason);
reject(new Error(String(this.reason)));
}
if (signal.aborted) {
handleAbort.call(signal);
Expand Down
10 changes: 5 additions & 5 deletions packages/rpc/src/__tests__/rpc-request-coalescer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('RPC request coalescer', () => {
return await new Promise((resolve, reject) => {
transportResponsePromise = resolve;
signal?.addEventListener('abort', (e: AbortSignalEventMap['abort']) => {
reject((e.target as AbortSignal).reason);
reject(new Error(String((e.target as AbortSignal).reason)));
});
});
});
Expand All @@ -129,13 +129,13 @@ describe('RPC request coalescer', () => {
expect.assertions(3);
abortControllerA.abort();
await expect(responsePromiseA).rejects.toThrow();
await expect(responsePromiseA).rejects.toBeInstanceOf(DOMException);
await expect(responsePromiseA).rejects.toHaveProperty('name', 'AbortError');
await expect(responsePromiseA).rejects.toBeInstanceOf(Error);
await expect(responsePromiseA).rejects.toHaveProperty('name', 'Error');
});
it("rejects from the aborted request with the `AbortSignal's` reason", async () => {
expect.assertions(1);
abortControllerA.abort('o no');
await expect(responsePromiseA).rejects.toBe('o no');
await expect(responsePromiseA).rejects.toThrow('o no');
});
it('aborts the transport at the end of the runloop when all of the requests abort', () => {
expect.assertions(1);
Expand All @@ -159,7 +159,7 @@ describe('RPC request coalescer', () => {
const mockResponse = { response: 'ok' };
transportResponsePromise(mockResponse);
await Promise.all([
expect(responsePromiseA).rejects.toBe('o no A'),
expect(responsePromiseA).rejects.toThrow('o no A'),
expect(responsePromiseB).resolves.toBe(mockResponse),
]);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/rpc-request-coalescer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function getRpcTransportWithRequestCoalescing<TTransport extends RpcTrans
abortController.abort((EXPLICIT_ABORT_TOKEN ||= createExplicitAbortToken()));
}
});
reject((e.target as AbortSignal).reason);
reject(new Error(String((e.target as AbortSignal).reason)));
};
signal.addEventListener('abort', handleAbort);
responsePromise
Expand Down