-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new fn safeWarpSync, safeWarp (#69)
* feat: new fn safeWarpSync, safeWarp * test: safeWarpSync and safeWarp * chore: 补充类型推断所需必要的 any * chore: 调整类型 * chore: 调整 TS 类型测试为注释 * chore: 避免不必要的规则检测
- Loading branch information
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
/** | ||
* 判断一个错误是否是网络错误, 如果是网络错误, 一般可以重试 | ||
|
@@ -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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters