aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/operation.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-02-15 16:59:56 -0300
committerSebastian <sebasjm@gmail.com>2024-02-15 16:59:56 -0300
commitfd45d189259cef0a3a51683bb12412cd8c6fb9eb (patch)
tree64091fbe1034a66a0caa6f4a1ef68ccbd3daab05 /packages/taler-util/src/operation.ts
parent8124d2400e7787020cca549ccc9d021eb5f75a09 (diff)
downloadwallet-core-fd45d189259cef0a3a51683bb12412cd8c6fb9eb.tar.xz
add exchange key in the http lib and default toString for taler error
Diffstat (limited to 'packages/taler-util/src/operation.ts')
-rw-r--r--packages/taler-util/src/operation.ts26
1 files changed, 23 insertions, 3 deletions
diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts
index a554e1f31..02cf70196 100644
--- a/packages/taler-util/src/operation.ts
+++ b/packages/taler-util/src/operation.ts
@@ -30,10 +30,14 @@ import {
TalerErrorDetail,
} from "./index.js";
-export type OperationResult<Body, ErrorEnum> =
+type OperationFailWithBodyOrNever<ErrorEnum, ErrorMap> =
+ ErrorEnum extends keyof ErrorMap ? OperationFailWithBody<ErrorMap> : never;
+
+export type OperationResult<Body, ErrorEnum, K = never> =
| OperationOk<Body>
- | OperationAlternative<ErrorEnum, Body>
- | OperationFail<ErrorEnum>;
+ | OperationAlternative<ErrorEnum, any>
+ | OperationFail<ErrorEnum>
+ | OperationFailWithBodyOrNever<ErrorEnum, K>;
export function isOperationOk<T, E>(
c: OperationResult<T, E>,
@@ -89,6 +93,15 @@ export interface OperationAlternative<T, B> {
body: B;
}
+export interface OperationFailWithBody<B> {
+ type: "fail";
+
+ httpResp: HttpResponse;
+
+ case: keyof B;
+ body: B[OperationFailWithBody<B>["case"]];
+}
+
export async function opSuccess<T>(
resp: HttpResponse,
codec: Codec<T>,
@@ -109,6 +122,13 @@ export function opEmptySuccess(resp: HttpResponse): OperationOk<void> {
return { type: "ok" as const, body: void 0, httpResp: resp };
}
+export async function opKnownFailureWithBody<B>(
+ case_: keyof B,
+ body: B[typeof case_],
+): Promise<OperationFailWithBody<B>> {
+ return { type: "fail", case: case_, body, httpResp: {} as any };
+}
+
export async function opKnownAlternativeFailure<T extends HttpStatusCode, B>(
resp: HttpResponse,
s: T,