From f0a02672a73f4512f11b561847d21920785caf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Aaron?= Date: Sun, 5 Feb 2023 14:40:54 +0100 Subject: [PATCH] fix: add hostname to lndhub errors --- .../background-script/connectors/lndhub.ts | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/extension/background-script/connectors/lndhub.ts b/src/extension/background-script/connectors/lndhub.ts index 06c6cb690f..e7a63355e7 100644 --- a/src/extension/background-script/connectors/lndhub.ts +++ b/src/extension/background-script/connectors/lndhub.ts @@ -1,5 +1,5 @@ -import axios, { AxiosRequestConfig, Method } from "axios"; -import type { AxiosResponse } from "axios"; +import type { AxiosResponse, Method } from "axios"; +import axios, { AxiosRequestConfig } from "axios"; import lightningPayReq from "bolt11"; import Base64 from "crypto-js/enc-base64"; import Hex from "crypto-js/enc-hex"; @@ -12,11 +12,11 @@ import state from "../state"; import Connector, { CheckPaymentArgs, CheckPaymentResponse, + ConnectorInvoice, + ConnectPeerResponse, GetBalanceResponse, GetInfoResponse, GetInvoicesResponse, - ConnectorInvoice, - ConnectPeerResponse, KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, @@ -330,34 +330,41 @@ export default class LndHub implements Connector { async authorize() { const url = `${this.config.url}/auth?type=auth`; - const { data: authData } = await axios.post( - url, - { - login: this.config.login, - password: this.config.password, - }, - { - headers: { - ...defaultHeaders, - "X-TS": Math.floor(Date.now() / 1000), - "X-VERIFY": this.generateHmacVerification(url), + try { + const { data: authData } = await axios.post( + url, + { + login: this.config.login, + password: this.config.password, }, - } - ); + { + headers: { + ...defaultHeaders, + "X-TS": Math.floor(Date.now() / 1000), + "X-VERIFY": this.generateHmacVerification(url), + }, + } + ); - if (authData.error || authData.errors) { - const error = authData.error || authData.errors; - const errMessage = error?.errors?.[0]?.message || error?.[0]?.message; + if (authData.error || authData.errors) { + const error = authData.error || authData.errors; + const errMessage = error?.errors?.[0]?.message || error?.[0]?.message; + + throw new Error(errMessage); + } - console.error(errMessage); - throw new Error("API error: " + errMessage); - } else { this.refresh_token = authData.refresh_token; this.access_token = authData.access_token; this.refresh_token_created = +new Date(); this.access_token_created = +new Date(); return authData; + } catch (e) { + throw new Error( + `API error (${this.config.url})${ + e instanceof Error ? `: ${e.message}` : "" + }` + ); } }