summaryrefslogtreecommitdiff
path: root/packages/taler-util/src/operation.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-util/src/operation.ts')
-rw-r--r--packages/taler-util/src/operation.ts39
1 files changed, 15 insertions, 24 deletions
diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts
index 02cf70196..ecf4a020a 100644
--- a/packages/taler-util/src/operation.ts
+++ b/packages/taler-util/src/operation.ts
@@ -57,8 +57,6 @@ export function isOperationFail<T, E>(
export interface OperationOk<BodyT> {
type: "ok";
- httpResp: HttpResponse;
-
/**
* Parsed response body.
*/
@@ -71,8 +69,6 @@ export interface OperationOk<BodyT> {
export interface OperationFail<T> {
type: "fail";
- httpResp: HttpResponse;
-
/**
* Error case (either HTTP status code or TalerErrorCode)
*/
@@ -87,8 +83,6 @@ export interface OperationFail<T> {
export interface OperationAlternative<T, B> {
type: "fail";
- httpResp: HttpResponse;
-
case: T;
body: B;
}
@@ -102,24 +96,24 @@ export interface OperationFailWithBody<B> {
body: B[OperationFailWithBody<B>["case"]];
}
-export async function opSuccess<T>(
+export async function opSuccessFromHttp<T>(
resp: HttpResponse,
codec: Codec<T>,
): Promise<OperationOk<T>> {
const body = await readSuccessResponseJsonOrThrow(resp, codec);
- return { type: "ok" as const, body, httpResp: resp };
+ return { type: "ok" as const, body };
}
/**
* Success case, but instead of the body we're returning a fixed response
* to the client.
*/
-export function opFixedSuccess<T>(resp: HttpResponse, body: T): OperationOk<T> {
- return { type: "ok" as const, body, httpResp: resp };
+export function opFixedSuccess<T>(body: T): OperationOk<T> {
+ return { type: "ok" as const, body };
}
export function opEmptySuccess(resp: HttpResponse): OperationOk<void> {
- return { type: "ok" as const, body: void 0, httpResp: resp };
+ return { type: "ok" as const, body: void 0 };
}
export async function opKnownFailureWithBody<B>(
@@ -135,7 +129,7 @@ export async function opKnownAlternativeFailure<T extends HttpStatusCode, B>(
codec: Codec<B>,
): Promise<OperationAlternative<T, B>> {
const body = await readSuccessResponseJsonOrThrow(resp, codec);
- return { type: "fail", case: s, body, httpResp: resp };
+ return { type: "fail", case: s, body };
}
export async function opKnownHttpFailure<T extends HttpStatusCode>(
@@ -143,7 +137,7 @@ export async function opKnownHttpFailure<T extends HttpStatusCode>(
resp: HttpResponse,
): Promise<OperationFail<T>> {
const detail = await readTalerErrorResponse(resp);
- return { type: "fail", case: s, detail, httpResp: resp };
+ return { type: "fail", case: s, detail };
}
export async function opKnownTalerFailure<T extends TalerErrorCode>(
@@ -151,7 +145,7 @@ export async function opKnownTalerFailure<T extends TalerErrorCode>(
resp: HttpResponse,
): Promise<OperationFail<T>> {
const detail = await readTalerErrorResponse(resp);
- return { type: "fail", case: s, detail, httpResp: resp };
+ return { type: "fail", case: s, detail };
}
export function opUnknownFailure(resp: HttpResponse, text: string): never {
@@ -171,24 +165,21 @@ export function opUnknownFailure(resp: HttpResponse, text: string): never {
* Convenience function to throw an error if the operation is not a success.
*/
export function narrowOpSuccessOrThrow<Body, ErrorEnum>(
+ opName: string,
opRes: OperationResult<Body, ErrorEnum>,
): asserts opRes is OperationOk<Body> {
- const httpResponse = opRes.httpResp;
if (opRes.type !== "ok") {
throw TalerError.fromDetail(
- TalerErrorCode.WALLET_UNEXPECTED_REQUEST_ERROR,
+ TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR,
{
- requestUrl: httpResponse.requestUrl,
- requestMethod: httpResponse.requestMethod,
- httpStatusCode: httpResponse.status,
- errorResponse:
+ operation: opName,
+ error: String(opRes.case),
+ detail:
"detail" in opRes
? opRes.detail
- : "body" in opRes
- ? opRes.body
- : undefined,
+ : undefined,
},
- `Unexpected HTTP status ${httpResponse.status} in response`,
+ `Operation ${opName} failed: ${String(opRes.case)}`,
);
}
}