8ce35a1e8e
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>
28 lines
929 B
TypeScript
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)
|
|
}
|
|
}
|