Skip to content

Commit

Permalink
fix(lnd): prevent undefined error
Browse files Browse the repository at this point in the history
This fix prevents an undefined error by better handling the case where
the lnd client attempts a call when it is not in a valid status to make
that call. Instead it throws the appropriate `UNAVAILABLE` error with
a helpful message.

Fixes #1367.
  • Loading branch information
sangaman committed Jan 23, 2020
1 parent ee37d38 commit 3ee94de
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions lib/lndclient/LndClient.ts
@@ -1,19 +1,19 @@
import assert from 'assert';
import { promises as fs, watch } from 'fs';
import grpc, { ChannelCredentials, ClientReadableStream } from 'grpc';
import path from 'path';
import { SwapClientType, SwapRole, SwapState } from '../constants/enums';
import Logger from '../Logger';
import SwapClient, { ClientStatus, SwapClientInfo, PaymentState, ChannelBalance, TradingLimits } from '../swaps/SwapClient';
import errors from './errors';
import swapErrors from '../swaps/errors';
import { LightningClient, WalletUnlockerClient } from '../proto/lndrpc_grpc_pb';
import { InvoicesClient } from '../proto/lndinvoices_grpc_pb';
import * as lndrpc from '../proto/lndrpc_pb';
import * as lndinvoices from '../proto/lndinvoices_pb';
import assert from 'assert';
import { promises as fs, watch } from 'fs';
import { SwapRole, SwapClientType, SwapState } from '../constants/enums';
import { LightningClient, WalletUnlockerClient } from '../proto/lndrpc_grpc_pb';
import * as lndrpc from '../proto/lndrpc_pb';
import swapErrors from '../swaps/errors';
import SwapClient, { ChannelBalance, ClientStatus, PaymentState, SwapClientInfo, TradingLimits } from '../swaps/SwapClient';
import { SwapDeal } from '../swaps/types';
import { base64ToHex, hexToUint8Array } from '../utils/utils';
import { LndClientConfig, LndInfo, ChannelCount, Chain, ClientMethods } from './types';
import path from 'path';
import errors from './errors';
import { Chain, ChannelCount, ClientMethods, LndClientConfig, LndInfo } from './types';

interface LndClient {
on(event: 'connectionVerified', listener: (swapClientInfo: SwapClientInfo) => void): this;
Expand Down Expand Up @@ -235,7 +235,11 @@ class LndClient extends SwapClient {
reject(errors.DISABLED);
return;
}
(this.lightning![methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (!this.lightning) {
reject(errors.UNAVAILABLE(this.currency, this.status));
return;
}
(this.lightning[methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (err) {
if (err.code === grpc.status.UNAVAILABLE) {
this.disconnect().catch(this.logger.error);
Expand Down Expand Up @@ -263,7 +267,11 @@ class LndClient extends SwapClient {
reject(errors.DISABLED);
return;
}
(this.invoices![methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (!this.invoices) {
reject(errors.UNAVAILABLE(this.currency, this.status));
return;
}
(this.invoices[methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (err) {
if (err.code === grpc.status.UNAVAILABLE) {
this.disconnect().catch(this.logger.error);
Expand All @@ -283,7 +291,11 @@ class LndClient extends SwapClient {
reject(errors.DISABLED);
return;
}
(this.walletUnlocker![methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (!this.walletUnlocker) {
reject(errors.UNAVAILABLE(this.currency, this.status));
return;
}
(this.walletUnlocker[methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (err) {
this.logger.trace(`error on ${methodName}: ${err.message}`);
reject(err);
Expand Down

0 comments on commit 3ee94de

Please sign in to comment.