feat(shared): add safeCreateHook utility for error-safe hook creation
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { log } from "./logger"
|
||||
|
||||
interface SafeCreateHookOptions {
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
export function safeCreateHook<T>(
|
||||
name: string,
|
||||
factory: () => T,
|
||||
options?: SafeCreateHookOptions,
|
||||
): T | null {
|
||||
const enabled = options?.enabled ?? true
|
||||
|
||||
if (!enabled) {
|
||||
return factory() ?? null
|
||||
}
|
||||
|
||||
try {
|
||||
return factory() ?? null
|
||||
} catch (error) {
|
||||
log(`[safe-create-hook] Hook creation failed: ${name}`, { error })
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user