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.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts
index 213bfeecd..a554e1f31 100644
--- a/packages/taler-util/src/operation.ts
+++ b/packages/taler-util/src/operation.ts
@@ -147,6 +147,32 @@ 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>(
+ 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,
+ {
+ requestUrl: httpResponse.requestUrl,
+ requestMethod: httpResponse.requestMethod,
+ httpStatusCode: httpResponse.status,
+ errorResponse:
+ "detail" in opRes
+ ? opRes.detail
+ : "body" in opRes
+ ? opRes.body
+ : undefined,
+ },
+ `Unexpected HTTP status ${httpResponse.status} in response`,
+ );
+ }
+}
+
export type ResultByMethod<
TT extends object,
p extends keyof TT,