Skip to content

Commit

Permalink
fix: add hostname to lndhub errors
Browse files Browse the repository at this point in the history
  • Loading branch information
reneaaron committed Feb 5, 2023
1 parent ba17e2c commit f0a0267
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/extension/background-script/connectors/lndhub.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,11 +12,11 @@ import state from "../state";
import Connector, {
CheckPaymentArgs,
CheckPaymentResponse,
ConnectorInvoice,
ConnectPeerResponse,
GetBalanceResponse,
GetInfoResponse,
GetInvoicesResponse,
ConnectorInvoice,
ConnectPeerResponse,
KeysendArgs,
MakeInvoiceArgs,
MakeInvoiceResponse,
Expand Down Expand Up @@ -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}` : ""
}`
);
}
}

Expand Down

0 comments on commit f0a0267

Please sign in to comment.