diff --git a/package-lock.json b/package-lock.json index c56e8c44..7d0cacc7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,7 +29,7 @@ "@types/puppeteer": "5.4.4", "@typescript-eslint/eslint-plugin": "^4.30.0", "@typescript-eslint/parser": "^4.0.0", - "@xrplf/eslint-config": "^1.3", + "@xrplf/eslint-config": "^1.3.0", "@xrplf/prettier-config": "^1.2.0", "assert": "^2.0.0", "buffer": "^6.0.2", @@ -58,8 +58,8 @@ "stream-browserify": "^3.0.0", "stream-http": "3.2.0", "ts-loader": "^9.2.5", - "ts-node": "^10.1.0", - "typescript": "^3.9.10", + "ts-node": "^10.2.1", + "typescript": "^4.4.2", "url": "^0.11.0", "webpack": "^5.6.0", "webpack-bundle-analyzer": "^4.1.0", @@ -8220,9 +8220,9 @@ "integrity": "sha512-7uc1O8h1M1g0rArakJdf0uLRSSgFcYexrVoKo+bzJd32gd4gDy2L/Z+8/FjPnU9ydY3pEnVPtr9FyscYY60K1g==" }, "node_modules/typescript": { - "version": "3.9.10", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", - "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", + "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -15207,9 +15207,9 @@ "integrity": "sha512-7uc1O8h1M1g0rArakJdf0uLRSSgFcYexrVoKo+bzJd32gd4gDy2L/Z+8/FjPnU9ydY3pEnVPtr9FyscYY60K1g==" }, "typescript": { - "version": "3.9.10", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", - "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", + "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==", "dev": true }, "unbox-primitive": { diff --git a/package.json b/package.json index a5d85d9e..557ed7e6 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "@types/puppeteer": "5.4.4", "@typescript-eslint/eslint-plugin": "^4.30.0", "@typescript-eslint/parser": "^4.0.0", - "@xrplf/eslint-config": "^1.3", + "@xrplf/eslint-config": "^1.3.0", "@xrplf/prettier-config": "^1.2.0", "assert": "^2.0.0", "buffer": "^6.0.2", @@ -68,8 +68,8 @@ "stream-browserify": "^3.0.0", "stream-http": "3.2.0", "ts-loader": "^9.2.5", - "ts-node": "^10.1.0", - "typescript": "^3.9.10", + "ts-node": "^10.2.1", + "typescript": "^4.4.2", "url": "^0.11.0", "webpack": "^5.6.0", "webpack-bundle-analyzer": "^4.1.0", diff --git a/src/client/connection.ts b/src/client/connection.ts index 6a40842a..a7215e90 100644 --- a/src/client/connection.ts +++ b/src/client/connection.ts @@ -349,8 +349,9 @@ export class Connection extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Must be a JSON dictionary data = JSON.parse(message) } catch (error) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- Errors have messages - this.emit('error', 'badMessage', error.message, message) + if (error instanceof Error) { + this.emit('error', 'badMessage', error.message, message) + } return } if (data.type == null && data.error) { @@ -366,8 +367,9 @@ export class Connection extends EventEmitter { try { this.requestManager.handleResponse(data) } catch (error) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- Errors have messages - this.emit('error', 'badMessage', error.message, message) + if (error instanceof Error) { + this.emit('error', 'badMessage', error.message, message) + } } } } @@ -435,10 +437,12 @@ export class Connection extends EventEmitter { this.connectionManager.resolveAllAwaiting() this.emit('connected') } catch (error) { - this.connectionManager.rejectAllAwaiting(error) - // Ignore this error, propagate the root cause. - // eslint-disable-next-line @typescript-eslint/no-empty-function -- Need empty catch - await this.disconnect().catch(() => {}) + if (error instanceof Error) { + this.connectionManager.rejectAllAwaiting(error) + // Ignore this error, propagate the root cause. + // eslint-disable-next-line @typescript-eslint/no-empty-function -- Need empty catch + await this.disconnect().catch(() => {}) + } } } diff --git a/src/client/requestManager.ts b/src/client/requestManager.ts index 4e73426c..4d0bb34f 100644 --- a/src/client/requestManager.ts +++ b/src/client/requestManager.ts @@ -17,8 +17,8 @@ export default class RequestManager { private readonly promisesAwaitingResponse = new Map< string | number, { - resolve: (value?: Response | PromiseLike) => void - reject: (value?: Error) => void + resolve: (value: Response | PromiseLike) => void + reject: (value: Error) => void timer: NodeJS.Timeout } >() @@ -109,11 +109,12 @@ export default class RequestManager { if (timer.unref) { timer.unref() } - const newPromise = new Promise( - (resolve: (value?: Response | PromiseLike) => void, reject) => { + const newPromise = new Promise( + (resolve: (value: Response | PromiseLike) => void, reject) => { this.promisesAwaitingResponse.set(newId, { resolve, reject, timer }) }, ) + return [newId, newRequest, newPromise] } diff --git a/src/wallet/generateFaucetWallet.ts b/src/wallet/generateFaucetWallet.ts index e0f4a0ed..117738b6 100644 --- a/src/wallet/generateFaucetWallet.ts +++ b/src/wallet/generateFaucetWallet.ts @@ -4,7 +4,7 @@ import { isValidClassicAddress } from 'ripple-address-codec' import type { Client } from '..' import { errors } from '../common' -import { RippledError } from '../common/errors' +import { RippledError, XRPLFaucetError } from '../common/errors' import { GeneratedAddress } from '../utils/generateAddress' import Wallet from '.' @@ -104,7 +104,7 @@ async function generateFaucetWallet( resolve(fundWallet) } else { reject( - new errors.XRPLFaucetError( + new XRPLFaucetError( `Unable to fund address with faucet after waiting ${ INTERVAL_SECONDS * MAX_ATTEMPTS } seconds`, @@ -112,11 +112,15 @@ async function generateFaucetWallet( ) } } catch (err) { - reject(new errors.XRPLFaucetError(err)) + if (err instanceof Error) { + reject(new XRPLFaucetError(err.message)) + } + + reject(err) } } else { reject( - new errors.XRPLFaucetError( + new XRPLFaucetError( `The faucet account classic address is undefined`, ), ) diff --git a/test/client/sign.ts b/test/client/sign.ts index 53a657db..119bd7c6 100644 --- a/test/client/sign.ts +++ b/test/client/sign.ts @@ -1,6 +1,7 @@ -import { assert } from 'chai' +import { assert, expect } from 'chai' import binary from 'ripple-binary-codec' +import { ValidationError } from '../../src/common/errors' import requests from '../fixtures/requests' import responses from '../fixtures/responses' import { setupClient, teardownClient } from '../setupClient' @@ -347,22 +348,29 @@ describe('client.sign', function () { Fee: '12', } const secret = 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV' + try { this.client.sign(JSON.stringify(offerCreate), secret) return await Promise.reject( new Error('this.client.sign should have thrown'), ) } catch (error) { - assert.equal(error.name, 'ValidationError') - assert.equal( - error.message, - 'Serialized transaction does not match original txJSON. See `error.data`', - ) - assert.deepEqual(error.data.diff, { - TakerGets: { - value: '3.14', - }, - }) + if (error instanceof ValidationError) { + assert.equal(error.name, 'ValidationError') + assert.equal( + error.message, + 'Serialized transaction does not match original txJSON. See `error.data`', + ) + expect(error.data).to.deep.include({ + diff: { + TakerGets: { + value: '3.14', + }, + }, + }) + } else { + assert(false) + } } }) }) diff --git a/test/client/subscribe.ts b/test/client/subscribe.ts index 66d2956a..aa3dba9a 100644 --- a/test/client/subscribe.ts +++ b/test/client/subscribe.ts @@ -1,7 +1,8 @@ import { assert } from 'chai' import rippled from '../fixtures/rippled' -import { setupClient, teardownClient } from '../setupClient' +import {setupClient, teardownClient} from '../setupClient' + describe('Subscription', function () { beforeEach(setupClient) diff --git a/test/connection.ts b/test/connection.ts index 44fc0608..dda246f9 100644 --- a/test/connection.ts +++ b/test/connection.ts @@ -262,6 +262,10 @@ describe('Connection', function () { try { await this.client.connect() } catch (error) { + if (!(error instanceof Error)) { + throw error + } + assert.instanceOf(error, DisconnectedError) assert.strictEqual( error.message, diff --git a/test/mockRippled.ts b/test/mockRippled.ts index 605b5e7d..7d5169e2 100644 --- a/test/mockRippled.ts +++ b/test/mockRippled.ts @@ -78,9 +78,12 @@ export default function createMockRippled(port: number): MockedWebSocketServer { ) } } catch (err) { + if (!(err instanceof Error)) { + throw err + } + if (!mock.suppressOutput) { - // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions -- Error - console.error(`Error: ${err.message}`) + console.error(err.message) } if (request != null) { conn.send( diff --git a/test/testUtils.ts b/test/testUtils.ts index 66f1b375..b0c68f1c 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -66,6 +66,10 @@ export async function assertRejects( await promise assert(false, 'Expected an error to be thrown') } catch (error) { + if (!(error instanceof Error)) { + throw error + } + assert(error instanceof instanceOf, error.message) if (typeof message === 'string') { assert.strictEqual(error.message, message) diff --git a/test/utils/computeLedgerHash.ts b/test/utils/computeLedgerHash.ts index 52d6ff1c..ee5a0ed3 100644 --- a/test/utils/computeLedgerHash.ts +++ b/test/utils/computeLedgerHash.ts @@ -1,6 +1,6 @@ import { assert } from 'chai' -import { ValidationError } from '../../src/common/errors' +import { ValidationError, XrplError } from '../../src/common/errors' import { computeLedgerHash } from '../../src/utils' import requests from '../fixtures/requests' import responses from '../fixtures/responses' @@ -27,6 +27,10 @@ describe('computeLedgerHash', function () { try { hash = computeLedgerHash(ledger, { computeTreeHashes: true }) } catch (error) { + if (!(error instanceof XrplError)) { + throw error + } + assert(error instanceof ValidationError) if (error instanceof ValidationError) { assert.strictEqual(