refactor: adds error response object (#1619)

* add error response object

* export error response properly

* type mockRippled

* fix linter

* fix ts

* fix comments
This commit is contained in:
Mayukha Vadari
2021-09-16 16:44:44 -04:00
parent 10445cff01
commit eb0445817e
26 changed files with 103 additions and 56 deletions

View File

@@ -36,7 +36,7 @@ module.exports = {
],
'max-lines-per-function': [
'warn',
{ max: 40, skipBlankLines: 'true', skipComments: 'true' },
{ max: 40, skipBlankLines: true, skipComments: true },
],
'max-statements': ['warn', 25],
'id-length': ['error', { exceptions: ['_'] }], // exception for lodash

View File

@@ -4,7 +4,7 @@ import {
TimeoutError,
} from '../common/errors'
import { Response } from '../models/methods'
import { BaseRequest } from '../models/methods/baseMethod'
import { BaseRequest, ErrorResponse } from '../models/methods/baseMethod'
/**
* Manage all the requests made to the websocket, and their async responses
@@ -126,7 +126,7 @@ export default class RequestManager {
* @param response - The response to handle.
* @throws ResponseFormatError if the response format is invalid, RippledError if rippled returns an error.
*/
public handleResponse(response: Partial<Response>): void {
public handleResponse(response: Partial<Response | ErrorResponse>): void {
if (
response.id == null ||
!Number.isInteger(response.id) ||
@@ -142,9 +142,11 @@ export default class RequestManager {
this.reject(response.id, error)
}
if (response.status === 'error') {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We know this must be true
const errorResponse = response as Partial<ErrorResponse>
const error = new RippledError(
response.error_message ?? response.error,
response,
errorResponse.error_message ?? errorResponse.error,
errorResponse,
)
this.reject(response.id, error)
return

View File

@@ -1,3 +1,5 @@
import type { Request } from '.'
export interface BaseRequest {
[x: string]: unknown
id?: number | string
@@ -13,15 +15,22 @@ interface Warning {
export interface BaseResponse {
id: number | string
status: 'success' | 'error' | string
status: 'success' | string
type: 'response' | string
result: unknown
warning?: 'load'
warnings?: Warning[]
forwarded?: boolean
error?: string
error_message?: string
// TODO: type this better
request?: unknown
api_version?: number
}
export interface ErrorResponse {
id: number | string
status: 'error'
type: 'response' | string
error: string
error_code?: string
error_message?: string
request: Request
api_version?: number
}

View File

@@ -1,5 +1,3 @@
/* eslint-disable import/max-dependencies -- All methods need to be exported */
import {
AccountChannelsRequest,
AccountChannelsResponse,
@@ -13,6 +11,7 @@ import { AccountLinesRequest, AccountLinesResponse } from './accountLines'
import { AccountObjectsRequest, AccountObjectsResponse } from './accountObjects'
import { AccountOffersRequest, AccountOffersResponse } from './accountOffers'
import { AccountTxRequest, AccountTxResponse } from './accountTx'
import { ErrorResponse } from './baseMethod'
import { BookOffersRequest, BookOffersResponse } from './bookOffers'
import { ChannelVerifyRequest, ChannelVerifyResponse } from './channelVerify'
import {
@@ -224,4 +223,5 @@ export {
PingResponse,
RandomRequest,
RandomResponse,
ErrorResponse,
}

View File

@@ -1,12 +1,11 @@
/* eslint-disable complexity -- verifies 19 tx types hence a lot of checks needed */
/* eslint-disable max-lines-per-function -- need to work with a lot of Tx verifications */
/* eslint-disable import/max-dependencies -- need to test more than 5 TxTypes */
import _ from 'lodash'
import { encode, decode } from 'ripple-binary-codec'
import { ValidationError } from '../../common/errors'
import TransactionMetadata from './metadata'
import { setTransactionFlagsToNumber } from '../utils'
import { AccountDelete, verifyAccountDelete } from './accountDelete'
import {
@@ -22,12 +21,14 @@ import { DepositPreauth, verifyDepositPreauth } from './depositPreauth'
import { EscrowCancel, verifyEscrowCancel } from './escrowCancel'
import { EscrowCreate, verifyEscrowCreate } from './escrowCreate'
import { EscrowFinish, verifyEscrowFinish } from './escrowFinish'
import TransactionMetadata from './metadata'
import { OfferCancel, verifyOfferCancel } from './offerCancel'
import {
OfferCreate,
verifyOfferCreate,
OfferCreateTransactionFlags,
} from './offerCreate'
import { Payment, verifyPayment, PaymentTransactionFlags } from './payment'
import {
PaymentChannelClaim,
verifyPaymentChannelClaim,
@@ -41,12 +42,10 @@ import {
PaymentChannelFund,
verifyPaymentChannelFund,
} from './paymentChannelFund'
import { Payment, verifyPayment, PaymentTransactionFlags } from './payment'
import { SetRegularKey, verifySetRegularKey } from './setRegularKey'
import { SignerListSet, verifySignerListSet } from './signerListSet'
import { TicketCreate, verifyTicketCreate } from './ticketCreate'
import { TrustSet, verifyTrustSet, TrustSetTransactionFlags } from './trustSet'
import { setTransactionFlagsToNumber } from '../utils'
export type Transaction =
| AccountDelete
@@ -78,7 +77,7 @@ export interface TransactionAndMetadata {
* Verifies various Transaction Types.
* Encode/decode and individual type validation.
*
* @param tx - A Transaction.
* @param transaction - A Transaction.
* @throws ValidationError When the Transaction is malformed.
*/
export function verify(transaction: Record<string, unknown>): void {

View File

@@ -73,7 +73,7 @@ class Wallet {
}
/**
* Derives a wallet from a secret (AKA a seed)
* Derives a wallet from a secret (AKA a seed).
*
* @param secret - A string used to generate a keypair (publicKey/privateKey) to derive a wallet.
* @param algorithm - The digital signature algorithm to generate an address fro.

View File

@@ -1,8 +1,7 @@
import { assert } from 'chai'
import rippled from '../fixtures/rippled'
import {setupClient, teardownClient} from '../setupClient'
import { setupClient, teardownClient } from '../setupClient'
describe('Subscription', function () {
beforeEach(setupClient)

View File

@@ -7,6 +7,7 @@ import { Client, Wallet } from 'xrpl-local'
import { AccountSet, SignerListSet } from 'xrpl-local/models/transactions'
import { convertStringToHex } from 'xrpl-local/utils'
import { sign, multisign } from 'xrpl-local/wallet/signer'
import { Transaction } from '../../src/models/transactions'
import serverUrl from './serverUrl'

View File

@@ -1,5 +1,6 @@
import { assert } from 'chai'
import _ from 'lodash'
import { Client } from 'xrpl-local'
import serverUrl from '../serverUrl'

View File

@@ -3,7 +3,10 @@ import _ from 'lodash'
import { Server as WebSocketServer } from 'ws'
import type { Request } from '../src'
import type { BaseResponse } from '../src/models/methods/baseMethod'
import type {
BaseResponse,
ErrorResponse,
} from '../src/models/methods/baseMethod'
import { getFreePort } from './testUtils'
@@ -83,6 +86,7 @@ export default function createMockRippled(port: number): MockedWebSocketServer {
}
if (!mock.suppressOutput) {
// eslint-disable-next-line no-console -- only printed out on error
console.error(err.message)
}
if (request != null) {
@@ -104,8 +108,9 @@ export default function createMockRippled(port: number): MockedWebSocketServer {
mock.addResponse = function (
command: string,
response:
| Record<string, unknown>
| ((r: Request) => Record<string, unknown>),
| Response
| ErrorResponse
| ((r: Request) => Response | ErrorResponse),
): void {
if (typeof command !== 'string') {
throw new Error('command is not a string')

View File

@@ -1,8 +1,10 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyAccountDelete } from './../../src/models/transactions/accountDelete'
import { verify } from './../../src/models/transactions'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyAccountDelete } from '../../src/models/transactions/accountDelete'
/**
* AccountDelete Transaction Verification Testing.
*

View File

@@ -1,8 +1,10 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyAccountSet } from './../../src/models/transactions/accountSet'
import { verify } from '../../src/models/transactions'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyAccountSet } from '../../src/models/transactions/accountSet'
/**
* AccountSet Transaction Verification Testing.
*

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyCheckCancel } from './../../src/models/transactions/checkCancel'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyCheckCancel } from '../../src/models/transactions/checkCancel'
/**
* CheckCancel Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyCheckCash } from './../../src/models/transactions/checkCash'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyCheckCash } from '../../src/models/transactions/checkCash'
/**
* CheckCash Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyCheckCreate } from './../../src/models/transactions/checkCreate'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyCheckCreate } from '../../src/models/transactions/checkCreate'
/**
* CheckCreate Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyDepositPreauth } from './../../src/models/transactions/depositPreauth'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyDepositPreauth } from '../../src/models/transactions/depositPreauth'
/**
* DepositPreauth Transaction Verification Testing.

View File

@@ -1,7 +1,8 @@
import { verifyEscrowCancel } from './../../src/models/transactions/escrowCancel'
import { assert } from 'chai'
import { ValidationError } from '../../src/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyEscrowCancel } from '../../src/models/transactions/escrowCancel'
/**
* Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyEscrowCreate } from './../../src/models/transactions/escrowCreate'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyEscrowCreate } from '../../src/models/transactions/escrowCreate'
/**
* EscrowCreate Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyEscrowFinish } from './../../src/models/transactions/escrowFinish'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyEscrowFinish } from '../../src/models/transactions/escrowFinish'
/**
* EscrowFinish Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyOfferCancel } from './../../src/models/transactions/offerCancel'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyOfferCancel } from '../../src/models/transactions/offerCancel'
/**
* OfferCancel Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyOfferCreate } from './../../src/models/transactions/offerCreate'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyOfferCreate } from '../../src/models/transactions/offerCreate'
/**
* OfferCreate Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyPaymentChannelClaim } from './../../src/models/transactions/paymentChannelClaim'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyPaymentChannelClaim } from '../../src/models/transactions/paymentChannelClaim'
/**
* PaymentChannelClaim Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyPaymentChannelCreate } from './../../src/models/transactions/paymentChannelCreate'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyPaymentChannelCreate } from '../../src/models/transactions/paymentChannelCreate'
/**
* PaymentChannelCreate Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyPaymentChannelFund } from './../../src/models/transactions/paymentChannelFund'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyPaymentChannelFund } from '../../src/models/transactions/paymentChannelFund'
/**
* PaymentChannelFund Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyTicketCreate } from './../../src/models/transactions/ticketCreate'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyTicketCreate } from '../../src/models/transactions/ticketCreate'
/**
* TicketCreate Transaction Verification Testing.

View File

@@ -1,7 +1,9 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyTrustSet } from './../../src/models/transactions/trustSet'
import { assert } from 'chai'
import { ValidationError } from 'xrpl-local/common/errors'
import { verify } from '../../src/models/transactions'
import { verifyTrustSet } from '../../src/models/transactions/trustSet'
/**
* TrustSet Transaction Verification Testing.