diff options
author | Florian Dold <florian@dold.me> | 2021-03-10 12:00:30 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-03-10 12:00:30 +0100 |
commit | ac89c3d277134e49e44d8b0afd4930fd4df934aa (patch) | |
tree | 2d2682630e108067d4f5f00946da681e978aa41c /packages/taler-wallet-core/src/util | |
parent | 49b5d006db6639082eea10158e2da7cc13473c21 (diff) | |
download | wallet-core-ac89c3d277134e49e44d8b0afd4930fd4df934aa.tar.xz |
restructure sync, store errors
Diffstat (limited to 'packages/taler-wallet-core/src/util')
-rw-r--r-- | packages/taler-wallet-core/src/util/http.ts | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/packages/taler-wallet-core/src/util/http.ts b/packages/taler-wallet-core/src/util/http.ts index 43a0ab16c..73f08d407 100644 --- a/packages/taler-wallet-core/src/util/http.ts +++ b/packages/taler-wallet-core/src/util/http.ts @@ -36,6 +36,7 @@ import { timestampMin, timestampMax, } from "./time"; +import { TalerErrorDetails } from ".."; const logger = new Logger("http.ts"); @@ -134,29 +135,35 @@ type ResponseOrError<T> = | { isError: false; response: T } | { isError: true; talerErrorResponse: TalerErrorResponse }; +export async function readTalerErrorResponse( + httpResponse: HttpResponse, +): Promise<TalerErrorDetails> { + const errJson = await httpResponse.json(); + const talerErrorCode = errJson.code; + if (typeof talerErrorCode !== "number") { + throw new OperationFailedError( + makeErrorDetails( + TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, + "Error response did not contain error code", + { + requestUrl: httpResponse.requestUrl, + requestMethod: httpResponse.requestMethod, + httpStatusCode: httpResponse.status, + }, + ), + ); + } + return errJson; +} + export async function readSuccessResponseJsonOrErrorCode<T>( httpResponse: HttpResponse, codec: Codec<T>, ): Promise<ResponseOrError<T>> { if (!(httpResponse.status >= 200 && httpResponse.status < 300)) { - const errJson = await httpResponse.json(); - const talerErrorCode = errJson.code; - if (typeof talerErrorCode !== "number") { - throw new OperationFailedError( - makeErrorDetails( - TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, - "Error response did not contain error code", - { - requestUrl: httpResponse.requestUrl, - requestMethod: httpResponse.requestMethod, - httpStatusCode: httpResponse.status, - }, - ), - ); - } return { isError: true, - talerErrorResponse: errJson, + talerErrorResponse: await readTalerErrorResponse(httpResponse), }; } const respJson = await httpResponse.json(); |