diff options
author | Florian Dold <florian.dold@gmail.com> | 2020-07-22 14:22:03 +0530 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2020-07-22 14:22:03 +0530 |
commit | e60563fb540c04d9ba751fea69c1fc0f1de598b5 (patch) | |
tree | 45f7c86b66dc150d413f9855efaa6341e4a44624 /src/headless | |
parent | f4a8702b3cf93f9edf96d1d1c8cb88baa309e301 (diff) |
consistent error handling for HTTP request (and some other things)
Diffstat (limited to 'src/headless')
-rw-r--r-- | src/headless/NodeHttpLib.ts | 36 | ||||
-rw-r--r-- | src/headless/helpers.ts | 5 | ||||
-rw-r--r-- | src/headless/integrationtest.ts | 7 | ||||
-rw-r--r-- | src/headless/merchant.ts | 8 | ||||
-rw-r--r-- | src/headless/taler-wallet-cli.ts | 7 |
5 files changed, 51 insertions, 12 deletions
diff --git a/src/headless/NodeHttpLib.ts b/src/headless/NodeHttpLib.ts index 118fb9e96..d109c3b7c 100644 --- a/src/headless/NodeHttpLib.ts +++ b/src/headless/NodeHttpLib.ts @@ -27,6 +27,8 @@ import { } from "../util/http"; import { RequestThrottler } from "../util/RequestThrottler"; import Axios from "axios"; +import { OperationFailedError, makeErrorDetails } from "../operations/errors"; +import { TalerErrorCode } from "../TalerErrorCode"; /** * Implementation of the HTTP request library interface for node. @@ -63,17 +65,44 @@ export class NodeHttpLib implements HttpRequestLibrary { const respText = resp.data; if (typeof respText !== "string") { - throw Error("unexpected response type"); + throw new OperationFailedError( + makeErrorDetails( + TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, + "unexpected response type", + { + httpStatusCode: resp.status, + requestUrl: url, + }, + ), + ); } const makeJson = async (): Promise<any> => { let responseJson; try { responseJson = JSON.parse(respText); } catch (e) { - throw Error("Invalid JSON from HTTP response"); + throw new OperationFailedError( + makeErrorDetails( + TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, + "invalid JSON", + { + httpStatusCode: resp.status, + requestUrl: url, + }, + ), + ); } if (responseJson === null || typeof responseJson !== "object") { - throw Error("Invalid JSON from HTTP response"); + throw new OperationFailedError( + makeErrorDetails( + TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE, + "invalid JSON", + { + httpStatusCode: resp.status, + requestUrl: url, + }, + ), + ); } return responseJson; }; @@ -82,6 +111,7 @@ export class NodeHttpLib implements HttpRequestLibrary { headers.set(hn, resp.headers[hn]); } return { + requestUrl: url, headers, status: resp.status, text: async () => resp.data, diff --git a/src/headless/helpers.ts b/src/headless/helpers.ts index 67ba62df8..e451db55b 100644 --- a/src/headless/helpers.ts +++ b/src/headless/helpers.ts @@ -143,7 +143,10 @@ export async function withdrawTestBalance( exchangeBaseUrl = "https://exchange.test.taler.net/", ): Promise<void> { await myWallet.updateExchangeFromUrl(exchangeBaseUrl, true); - const reserveResponse = await myWallet.acceptManualWithdrawal(exchangeBaseUrl, Amounts.parseOrThrow(amount)); + const reserveResponse = await myWallet.acceptManualWithdrawal( + exchangeBaseUrl, + Amounts.parseOrThrow(amount), + ); const reservePub = reserveResponse.reservePub; diff --git a/src/headless/integrationtest.ts b/src/headless/integrationtest.ts index 786907a09..db96d57c4 100644 --- a/src/headless/integrationtest.ts +++ b/src/headless/integrationtest.ts @@ -25,7 +25,6 @@ import { NodeHttpLib } from "./NodeHttpLib"; import { Wallet } from "../wallet"; import { Configuration } from "../util/talerconfig"; import { Amounts, AmountJson } from "../util/amounts"; -import { OperationFailedAndReportedError, OperationFailedError } from "../operations/errors"; const logger = new Logger("integrationtest.ts"); @@ -70,9 +69,9 @@ async function makePayment( } const confirmPayResult = await wallet.confirmPay( - preparePayResult.proposalId, - undefined, - ); + preparePayResult.proposalId, + undefined, + ); console.log("confirmPayResult", confirmPayResult); diff --git a/src/headless/merchant.ts b/src/headless/merchant.ts index 3324924fd..34ca5564d 100644 --- a/src/headless/merchant.ts +++ b/src/headless/merchant.ts @@ -37,7 +37,10 @@ export class MerchantBackendConnection { reason: string, refundAmount: string, ): Promise<string> { - const reqUrl = new URL(`private/orders/${orderId}/refund`, this.merchantBaseUrl); + const reqUrl = new URL( + `private/orders/${orderId}/refund`, + this.merchantBaseUrl, + ); const refundReq = { reason, refund: refundAmount, @@ -123,7 +126,8 @@ export class MerchantBackendConnection { } async checkPayment(orderId: string): Promise<CheckPaymentResponse> { - const reqUrl = new URL(`private/orders/${orderId}`, this.merchantBaseUrl).href; + const reqUrl = new URL(`private/orders/${orderId}`, this.merchantBaseUrl) + .href; const resp = await axios({ method: "get", url: reqUrl, diff --git a/src/headless/taler-wallet-cli.ts b/src/headless/taler-wallet-cli.ts index fc13d77f3..b8ae84d72 100644 --- a/src/headless/taler-wallet-cli.ts +++ b/src/headless/taler-wallet-cli.ts @@ -30,7 +30,7 @@ import { setupRefreshPlanchet, encodeCrock, } from "../crypto/talerCrypto"; -import { OperationFailedAndReportedError, OperationFailedError } from "../operations/errors"; +import { OperationFailedAndReportedError } from "../operations/errors"; import { Bank } from "./bank"; import { classifyTalerUri, TalerUriType } from "../util/taleruri"; import { Configuration } from "../util/talerconfig"; @@ -527,7 +527,10 @@ advancedCli .maybeOption("sessionIdOverride", ["--session-id"], clk.STRING) .action(async (args) => { await withWallet(args, async (wallet) => { - wallet.confirmPay(args.payConfirm.proposalId, args.payConfirm.sessionIdOverride); + wallet.confirmPay( + args.payConfirm.proposalId, + args.payConfirm.sessionIdOverride, + ); }); }); |