From e60563fb540c04d9ba751fea69c1fc0f1de598b5 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 22 Jul 2020 14:22:03 +0530 Subject: consistent error handling for HTTP request (and some other things) --- src/headless/NodeHttpLib.ts | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'src/headless/NodeHttpLib.ts') 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 => { 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, -- cgit v1.2.3