import toast from 'react-hot-toast' type ExcludeErr = Exclude // Used to bubble errors up export function err(value: ExcludeErr | Error): value is Error { if (!(value instanceof Error)) { return false } return true } /** Takes array of maybe error and types narrows them into * @returns [hasErr, arrayWithoutErr, arrayWithErr] */ export function cleanErrs( value: Array | Error> ): [boolean, Array>, Array] { const argsWOutErr: Array> = [] const argsWErr: Array = [] for (const v of value) { if (err(v)) { argsWErr.push(v) } else { argsWOutErr.push(v) } } return [argsWOutErr.length !== value.length, argsWOutErr, argsWErr] } // Used to report errors to user at a certain point in execution export function trap( value: ExcludeErr | Error, opts?: { altErr?: Error suppress?: boolean } ): value is Error { if (!err(value)) { return false } console.error(value) opts?.suppress || toast.error((opts?.altErr ?? value ?? new Error('Unknown')).toString()) return true }