Skip to content

Commit

Permalink
Enable prefer-promise-reject-errors rule and supress errors for highe…
Browse files Browse the repository at this point in the history
…r flexibility
  • Loading branch information
kox committed Jan 4, 2025
1 parent 8111c88 commit f59b494
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 3 deletions.
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(this.reason));
});
}
processQueue();
Expand Down
2 changes: 2 additions & 0 deletions packages/promises/src/abortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export function getAbortablePromise<T>(promise: Promise<T>, abortSignal?: AbortS
// want to throw even if the input promise's result is ready
new Promise<never>((_, reject) => {
if (abortSignal.aborted) {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(abortSignal.reason);
} else {
abortSignal.addEventListener('abort', function () {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(this.reason);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function createWebSocketChannel({
url,
}: Config): Promise<RpcSubscriptionsChannel<WebSocketMessage, string>> {
if (signal.aborted) {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
return Promise.reject(signal.reason);
}
let bufferDrainWatcher: Readonly<{ onCancel(): void; promise: Promise<void> }> | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export async function executeRpcPubSubSubscriptionPlan<TNotification>({
subscriptionId = undefined;
channel.send(unsubscribePayload).catch(() => {});
}
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(this.reason);
}
if (signal.aborted) {
Expand Down
2 changes: 1 addition & 1 deletion 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((e.target as AbortSignal).reason));
});
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/rpc/src/rpc-request-coalescer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function getRpcTransportWithRequestCoalescing<TTransport extends RpcTrans
abortController.abort((EXPLICIT_ABORT_TOKEN ||= createExplicitAbortToken()));
}
});
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject((e.target as AbortSignal).reason);
};
signal.addEventListener('abort', handleAbort);
Expand Down

0 comments on commit f59b494

Please sign in to comment.