18 lines
504 B
TypeScript
18 lines
504 B
TypeScript
import { fail } from './http.js'
|
|
|
|
export type Context = Record<string, unknown>
|
|
|
|
export type FunctionHandler = (request: Request, context: Context) => Promise<Response> | Response
|
|
|
|
export const withErrorBoundary = (handler: FunctionHandler): FunctionHandler => {
|
|
return async (request, context) => {
|
|
try {
|
|
return await handler(request, context)
|
|
} catch (error) {
|
|
console.error(error)
|
|
return fail('服务异常,请稍后再试', 500, 'INTERNAL_SERVER_ERROR')
|
|
}
|
|
}
|
|
}
|
|
|