diff options
author | Sebastian <sebasjm@gmail.com> | 2024-09-02 14:59:09 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-09-02 14:59:23 -0300 |
commit | b52b13ddae1dfcb2866f0e55de5617681165b7de (patch) | |
tree | ef127ca69f125cb57b36340ad628fb255b6667bd /packages/taler-util/src/operation.ts | |
parent | 5bdefcf53d0bbad5b3c9474fab9b0cfa6eafeae9 (diff) |
when error happen there is no garantee that taler error details is present, so undefined should be set
Diffstat (limited to 'packages/taler-util/src/operation.ts')
-rw-r--r-- | packages/taler-util/src/operation.ts | 67 |
1 files changed, 48 insertions, 19 deletions
diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts index 6b5eb61a6..d757fd529 100644 --- a/packages/taler-util/src/operation.ts +++ b/packages/taler-util/src/operation.ts @@ -31,14 +31,14 @@ import { TalerErrorDetail, } from "./index.js"; -type OperationFailWithBodyOrNever<ErrorEnum, ErrorMap> = - ErrorEnum extends keyof ErrorMap ? OperationFailWithBody<ErrorMap> : never; +// type OperationFailWithBodyOrNever<ErrorEnum, ErrorMap> = +// ErrorEnum extends keyof ErrorMap ? OperationFailWithBody<ErrorMap> : never; export type OperationResult<Body, ErrorEnum, K = never> = | OperationOk<Body> | OperationAlternative<ErrorEnum, any> | OperationFail<ErrorEnum> - | OperationFailWithBodyOrNever<ErrorEnum, K>; +// | OperationFailWithBodyOrNever<ErrorEnum, K>; export function isOperationOk<T, E>( c: OperationResult<T, E>, @@ -75,7 +75,7 @@ export interface OperationFail<T> { */ case: T; - detail: TalerErrorDetail; + detail?: TalerErrorDetail; } /** @@ -88,12 +88,12 @@ export interface OperationAlternative<T, B> { body: B; } -export interface OperationFailWithBody<B> { - type: "fail"; +// export interface OperationFailWithBody<B> { +// type: "fail"; - case: keyof B; - body: B[OperationFailWithBody<B>["case"]]; -} +// case: keyof B; +// body: B[OperationFailWithBody<B>["case"]]; +// } export async function opSuccessFromHttp<T>( resp: HttpResponse, @@ -115,10 +115,15 @@ export function opEmptySuccess(resp: HttpResponse): OperationOk<void> { return { type: "ok" as const, body: void 0 }; } -export async function opKnownFailureWithBody<B>( - case_: keyof B, - body: B[typeof case_], -): Promise<OperationFailWithBody<B>> { +export async function opKnownFailure<T>( + case_: T): Promise<OperationFail<T>> { + return { type: "fail", case: case_ }; +} + +export async function opKnownFailureWithBody<T,B>( + case_: T, + body: B, +): Promise<OperationAlternative<T, B>> { return { type: "fail", case: case_, body }; } @@ -182,8 +187,8 @@ export function narrowOpSuccessOrThrow<Body, ErrorEnum>( } } -export async function succeedOrThrow<R, E>( - promise: Promise<OperationResult<R, E>>, +export async function succeedOrThrow<R>( + promise: Promise<OperationResult<R, unknown>>, ): Promise<R> { const resp = await promise; if (isOperationOk(resp)) { @@ -196,6 +201,30 @@ export async function succeedOrThrow<R, E>( throw TalerError.fromException(resp); } +export async function alternativeOrThrow<Error,Body, Alt>( + s: Error, + promise: Promise<OperationOk<Body> + | OperationAlternative<Error, Alt> + | OperationFail<Error>>, +): Promise<Alt> { + const resp = await promise; + if (isOperationOk(resp)) { + throw TalerError.fromException( + new Error(`request succeed but failure "${s}" was expected`), + ); + } + if (isOperationFail(resp) && resp.case !== s) { + throw TalerError.fromException( + new Error( + `request failed with "${JSON.stringify( + resp, + )}" but case "${s}" was expected`, + ), + ); + } + return (resp as any).body; +} + export async function failOrThrow<E>( s: E, promise: Promise<OperationResult<unknown, E>>, @@ -223,10 +252,10 @@ export type ResultByMethod< p extends keyof TT, > = TT[p] extends (...args: any[]) => infer Ret ? Ret extends Promise<infer Result> - ? Result extends OperationResult<any, any> - ? Result - : never - : never //api always use Promises + ? Result extends OperationResult<any, any> + ? Result + : never + : never //api always use Promises : never; //error cases just for functions export type FailCasesByMethod<TT extends object, p extends keyof TT> = Exclude< |