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/NodeHttpLib.ts | |
parent | f4a8702b3cf93f9edf96d1d1c8cb88baa309e301 (diff) | |
download | wallet-core-e60563fb540c04d9ba751fea69c1fc0f1de598b5.tar.xz |
consistent error handling for HTTP request (and some other things)
Diffstat (limited to 'src/headless/NodeHttpLib.ts')
-rw-r--r-- | src/headless/NodeHttpLib.ts | 36 |
1 files changed, 33 insertions, 3 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, |