Skip to content

Commit

Permalink
feat: new fn safeWarpSync, safeWarp (#69)
Browse files Browse the repository at this point in the history
* feat: new fn safeWarpSync, safeWarp

* test: safeWarpSync and safeWarp

* chore: 补充类型推断所需必要的 any

* chore: 调整类型

* chore: 调整 TS 类型测试为注释

* chore: 避免不必要的规则检测
  • Loading branch information
iugo authored Jun 6, 2024
1 parent 78a9b42 commit f160cd7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
24 changes: 23 additions & 1 deletion js/err.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertIsError } from 'https://deno.land/[email protected]/assert/assert_is_error.ts';
import { delay } from 'https://deno.land/[email protected]/async/delay.ts';
import { autoRetry } from './err.ts';
// deno-lint-ignore no-unused-vars
import { autoRetry, safeWarp, safeWarpSync } from './err.ts';

Deno.test('autoRetry', async () => {
async function test(time: number) {
Expand All @@ -17,3 +18,24 @@ Deno.test('autoRetry', async () => {
assertIsError(err, Error, 'retryed');
});
});

Deno.test('safeWarpSync and safeWarp', () => {
// 👇 手动查看类型符合预期
// (name: string) => number | void
// const nf1 = safeWarpSync((name: string) => 1, () => {});
// (name: string) => number
// const nf2 = safeWarpSync((name: string) => 1, () => 2);

// 👇 预期类型错误
// const nf3 = safeWarpSync((name: string) => Promise.resolve(1), () => 2);
// const nf4 = safeWarpSync(async (name: string) => 1, () => 2);

// 👇 手动查看类型符合预期
// (name: string) => Promise<void | number>
// const anf1 = safeWarp(async (name: string) => 1, () => {});
// (name: string) => Promise<number>
// const anf2 = safeWarp(async (name: string) => 1, () => 2);

// 👇 预期类型错误
// const anf3 = safeWarp((name: string) => 1, () => 2);
});
40 changes: 40 additions & 0 deletions js/err.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { delay } from 'https://deno.land/[email protected]/async/delay.ts';
import { NotPromise } from '../ts/object.ts';

/**
* 判断一个错误是否是网络错误, 如果是网络错误, 一般可以重试
Expand Down Expand Up @@ -68,3 +69,42 @@ export function autoRetry<T extends Array<any>, R>(
throw timeoutError;
};
}

/**
* 同步函数抛错处理
* @param fn 业务函数
* @param errHandler 错误处理函数
*/
export function safeWarpSync<
T extends (...args: Parameters<T>) => NotPromise<ReturnType<T>>,
R,
>(
fn: T,
errHandler: (err: unknown) => R,
): (...args: Parameters<T>) => ReturnType<T> | R {
return (...args) => {
try {
return fn(...args);
} catch (err) {
return errHandler(err);
}
};
}

/**
* 异步函数抛错处理
* @param fn 业务函数
* @param errHandler 错误处理函数
*/
export function safeWarp<
// deno-lint-ignore no-explicit-any
T extends (...args: Parameters<T>) => Promise<any>,
R,
>(
fn: T,
errHandler: (err: unknown) => R,
): (...args: Parameters<T>) => ReturnType<T> | Promise<R> {
return (...args) => {
return fn(...args).catch(errHandler);
};
}
5 changes: 5 additions & 0 deletions ts/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export type EmptyObject = Record<string, never>;
/** 未知对象 */
export type UnknownObject = Record<string, unknown>;

/**
* 非 Promise 对象
*/
export type NotPromise<T> = T extends Promise<infer _> ? never : T;

/** 判断是否为未知对象 */
export function isUnknownObject(v: unknown): v is UnknownObject {
if (typeof v !== 'object' || v === null) {
Expand Down

0 comments on commit f160cd7

Please sign in to comment.