Skip to content

Commit

Permalink
trpc catchError
Browse files Browse the repository at this point in the history
  • Loading branch information
LAMMUpro committed Jan 14, 2024
1 parent 7f79a47 commit 141fa44
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/trpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from '@lammu/amis-trpc/src/routes'; // 通过软链接等形式拿到后端的类型

/** trpc对象 */
/**
* trpc函数异常捕获(后续需要集成到)
* @param originRequest 原trpc函数
* @returns 带异常处理的函数
*/
export function catchError<T extends (...args: any) => any>(originRequest: T) {
async function catchRequest(...data: ParamsType<typeof originRequest>): Promise<ApiResponse<ReturnType<T>>> {
try {
const result = await originRequest.apply(data);
return {
success: true,
data: result,
}
} catch(error) {
console.warn('异常了', error);
return {
success: false,
code: 'errorCode',
msg: '错误提示',
msgTimeout: 3000
}
}
}
return catchRequest;
}

/**
* trpc对象
*/
export const trpc = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
Expand All @@ -10,4 +38,7 @@ export const trpc = createTRPCProxyClient<AppRouter>({
],
});

// console.log(trpc.userList.query());
// const res = await catchError(trpc.application.getListByPage.query)({
// currentPage: 1,
// pageSize: 10
// })

0 comments on commit 141fa44

Please sign in to comment.