Files
bilin/src/main/ipc/result.ts
T
bing 8ce35a1e8e feat: ship v0.3.0 with P2.1 editor polish and P3 AI collaboration
Add chapter reorder, search replace, inspiration capture, and AI chat with context editing, settings, offline handling, and naming checks. Fix dev black screen by keeping process.env reads in the main process only.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 16:33:56 +08:00

28 lines
929 B
TypeScript

import { ErrorCode, type IpcError, type IpcResult } from '../../shared/types'
export function ok<T>(data: T): IpcResult<T> {
return { ok: true, data }
}
export function fail(code: ErrorCode, message: string, recoverable: boolean): IpcResult<never> {
return { ok: false, error: { code, message, recoverable } }
}
export function wrap<T>(fn: () => T | Promise<T>, recoverable = true): IpcResult<T> | Promise<IpcResult<T>> {
try {
const out = fn()
if (out && typeof (out as Promise<T>).then === 'function') {
return (out as Promise<T>)
.then((data) => ok(data))
.catch((e) => {
const message = e instanceof Error ? e.message : String(e)
return fail(ErrorCode.UNKNOWN, message, recoverable)
})
}
return ok(out as T)
} catch (e) {
const message = e instanceof Error ? e.message : String(e)
return fail(ErrorCode.UNKNOWN, message, recoverable)
}
}