|
| 1 | +import { type Err, Result, ResultAsync, err, ok } from 'neverthrow' |
| 2 | + |
| 3 | +import { coreTags } from '../tag' |
| 4 | +import type { BlobType, Promisify } from '../utils/common' |
| 5 | + |
| 6 | +export class ProjectError extends Error { |
| 7 | + constructor( |
| 8 | + public name: Sirutils.Error[keyof Sirutils.Error], |
| 9 | + public message: string, |
| 10 | + public cause: BlobType[] = [] |
| 11 | + ) { |
| 12 | + super() |
| 13 | + } |
| 14 | + |
| 15 | + appendCause(additionalCause?: string) { |
| 16 | + if (additionalCause && this.cause[this.cause.length - 1] !== additionalCause) { |
| 17 | + this.cause.push(additionalCause) |
| 18 | + } |
| 19 | + |
| 20 | + return this |
| 21 | + } |
| 22 | + |
| 23 | + asResult(additionalCause?: string) { |
| 24 | + return err(this.appendCause(additionalCause)) |
| 25 | + } |
| 26 | + |
| 27 | + static create = ( |
| 28 | + name: Sirutils.Error[keyof Sirutils.Error], |
| 29 | + message: string, |
| 30 | + cause?: BlobType |
| 31 | + ) => { |
| 32 | + if (cause) { |
| 33 | + return new ProjectError(name, message, [cause]) |
| 34 | + } |
| 35 | + |
| 36 | + return new ProjectError(name, message) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +export const unwrap = <T, E extends Sirutils.ProjectErrorType>( |
| 41 | + result: Result<T, E>, |
| 42 | + additionalCause?: string |
| 43 | +): T | never => { |
| 44 | + if (result.isErr()) { |
| 45 | + if (additionalCause) { |
| 46 | + result.error.appendCause(additionalCause) |
| 47 | + } |
| 48 | + |
| 49 | + throw result.error |
| 50 | + } |
| 51 | + |
| 52 | + return result.value |
| 53 | +} |
| 54 | + |
| 55 | +export const group = <T, E extends Sirutils.ProjectErrorType>( |
| 56 | + body: () => T, |
| 57 | + additionalCause: string |
| 58 | +): Result<T, E> => { |
| 59 | + try { |
| 60 | + return ok(body()) |
| 61 | + } catch (e) { |
| 62 | + if (e instanceof ProjectError) { |
| 63 | + return e.asResult(additionalCause) as unknown as Err<T, E> |
| 64 | + } |
| 65 | + |
| 66 | + return ProjectError.create( |
| 67 | + coreTags.group, |
| 68 | + `${e}`, |
| 69 | + additionalCause |
| 70 | + ).asResult() as unknown as Err<T, E> |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export const groupAsync = async <T, E extends Sirutils.ProjectErrorType>( |
| 75 | + body: () => Promisify<T>, |
| 76 | + additionalCause: string |
| 77 | +): Promise<Result<T, E>> => { |
| 78 | + try { |
| 79 | + return ok(await body()) |
| 80 | + } catch (e) { |
| 81 | + if (e instanceof ProjectError) { |
| 82 | + return e.asResult(additionalCause) as unknown as Err<T, E> |
| 83 | + } |
| 84 | + |
| 85 | + return ProjectError.create( |
| 86 | + coreTags.groupAsync, |
| 87 | + `${e}`, |
| 88 | + additionalCause |
| 89 | + ).asResult() as unknown as Err<T, E> |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export const wrap = <A extends BlobType[], T, E extends Sirutils.ProjectErrorType>( |
| 94 | + body: (...args: A) => T, |
| 95 | + additionalCause: string |
| 96 | +) => { |
| 97 | + return Result.fromThrowable( |
| 98 | + body, |
| 99 | + e => |
| 100 | + (e instanceof ProjectError |
| 101 | + ? e.appendCause(additionalCause) |
| 102 | + : ProjectError.create(coreTags.wrap, `${e}`, additionalCause)) as E |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +export const wrapAsync = <A extends BlobType[], T, E extends Sirutils.ProjectErrorType>( |
| 107 | + body: (...args: A) => PromiseLike<T>, |
| 108 | + additionalCause: string |
| 109 | +) => { |
| 110 | + return (...args: A) => |
| 111 | + ResultAsync.fromPromise<T, E>( |
| 112 | + Promise.resolve(body(...args)), |
| 113 | + e => |
| 114 | + (e instanceof ProjectError |
| 115 | + ? e.appendCause(additionalCause) |
| 116 | + : ProjectError.create(coreTags.wrapAsync, `${e}`, additionalCause)) as E |
| 117 | + ) |
| 118 | +} |
0 commit comments