Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/kasper9n/ferrum. Music library app for Mac, Linux and Windows ferrum.kasper.space
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104import { ipc_renderer } from './window'
export function error_popup(err: unknown, crash = false) { ipc_renderer.invoke( 'showMessageBox', false, { type: 'error', message: parse_error(err).message, detail: error_to_string(err), }, crash, )}
function parse_error(raw_err: unknown): Error { let error: Error if (raw_err instanceof Error) { error = raw_err } else { error = new Error('Unexpected error: ' + String(raw_err)) } return error}
export function error_to_string(raw_err: unknown): string { const error = parse_error(raw_err) const causes: string[] = []
let cause = error.cause while (cause) { const cause_error = parse_error(cause) causes.push(cause_error.message) cause = cause_error.cause }
let result = error.message
if (causes.length) { result += '\n\nCaused by:'
for (const [i, cause] of causes.entries()) { const prefix = causes.length > 1 ? `${i}: ` : '' result += `\n ${prefix}${cause}` } }
return result}
// Crashes on errorexport function strict_call<T>(cb: (addon: typeof window.addon) => T): T { try { const result = cb(window.addon)
// Handle async errors if (result instanceof Promise) { return result.catch((raw_err) => { const error = parse_error(raw_err) error_popup(error, true) throw error }) as T }
// Handle synchronous result return result } catch (raw_err) { const error = parse_error(raw_err) // Handle synchronous errors error_popup(error, true) throw error }}
type BaseResult<T> = { data: T; error: null } | { data: null; error: Error }type Result<T> = BaseResult<T> & { on_success: (cb: (data: T) => void) => BaseResult<T>}
// Shows a popup message on errorexport function call_sync<T>(cb: (addon: typeof window.addon) => T): Result<T> { const result = (() => { try { const data = cb(window.addon)
const result = { data, error: null } return result } catch (raw_err) { const error = parse_error(raw_err) error_popup(error)
const result = { data: null, error } return result } })() as Result<T> result.on_success = (cb) => { if (result.error === null) { cb(result.data) } return result } return result}