From b62108ec4b5b040a1a3e84635d8d4349411a05bd Mon Sep 17 00:00:00 2001 From: Nathan Richards Date: Fri, 19 Jul 2024 12:44:13 +0200 Subject: [PATCH] feat: remove htmlmessage prop from errors (#200) --- src/core/EVM/EVMStepExecutor.ts | 1 - src/core/EVM/checkAllowance.ts | 1 - src/core/EVM/parseEVMErrors.ts | 10 +--- src/core/Solana/SolanaStepExecutor.ts | 1 - src/core/Solana/parseSolanaErrors.ts | 17 ++----- src/core/checkBalance.ts | 2 +- src/core/stepComparison.ts | 4 +- src/errors/SDKError.unit.spec.ts | 1 - src/errors/baseError.ts | 10 +--- src/errors/baseError.unit.spec.ts | 1 - src/errors/errors.ts | 47 ++++--------------- src/errors/httpError.ts | 14 +++--- src/errors/httpError.unit.spec.ts | 15 ++---- .../utils/baseErrorRootCause.unit.spec.ts | 1 - src/errors/utils/rootCause.unit.spec.ts | 1 - 15 files changed, 27 insertions(+), 99 deletions(-) diff --git a/src/core/EVM/EVMStepExecutor.ts b/src/core/EVM/EVMStepExecutor.ts index 600deaa1..45bcd420 100644 --- a/src/core/EVM/EVMStepExecutor.ts +++ b/src/core/EVM/EVMStepExecutor.ts @@ -408,7 +408,6 @@ export class EVMStepExecutor extends BaseStepExecutor { { error: { message: error.cause.message, - htmlMessage: error.cause.htmlMessage, code: error.code, }, } diff --git a/src/core/EVM/checkAllowance.ts b/src/core/EVM/checkAllowance.ts index e3c634e0..6b20ac23 100644 --- a/src/core/EVM/checkAllowance.ts +++ b/src/core/EVM/checkAllowance.ts @@ -108,7 +108,6 @@ export const checkAllowance = async ( { error: { message: error.cause.message, - htmlMessage: error.cause.htmlMessage, code: error.code, }, } diff --git a/src/core/EVM/parseEVMErrors.ts b/src/core/EVM/parseEVMErrors.ts index 66b10e0a..7906d4e7 100644 --- a/src/core/EVM/parseEVMErrors.ts +++ b/src/core/EVM/parseEVMErrors.ts @@ -27,12 +27,7 @@ const handleSpecificErrors = async ( process?: Process ) => { if (e.cause?.name === 'UserRejectedRequestError') { - return new TransactionError( - LiFiErrorCode.SignatureRejected, - e.message, - undefined, - e - ) + return new TransactionError(LiFiErrorCode.SignatureRejected, e.message, e) } if ( @@ -52,7 +47,6 @@ const handleSpecificErrors = async ( return new TransactionError( LiFiErrorCode.GasLimitError, ErrorMessage.GasLimitLow, - undefined, e ) } @@ -62,5 +56,5 @@ const handleSpecificErrors = async ( return e } - return new UnknownError(e.message || ErrorMessage.UnknownError, undefined, e) + return new UnknownError(e.message || ErrorMessage.UnknownError, e) } diff --git a/src/core/Solana/SolanaStepExecutor.ts b/src/core/Solana/SolanaStepExecutor.ts index 9df07150..77c3d372 100644 --- a/src/core/Solana/SolanaStepExecutor.ts +++ b/src/core/Solana/SolanaStepExecutor.ts @@ -294,7 +294,6 @@ export class SolanaStepExecutor extends BaseStepExecutor { { error: { message: error.cause.message, - htmlMessage: error.cause.htmlMessage, code: error.code, }, } diff --git a/src/core/Solana/parseSolanaErrors.ts b/src/core/Solana/parseSolanaErrors.ts index 13e2dbbf..f993c7f4 100644 --- a/src/core/Solana/parseSolanaErrors.ts +++ b/src/core/Solana/parseSolanaErrors.ts @@ -22,28 +22,17 @@ export const parseSolanaErrors = async ( const handleSpecificErrors = (e: any) => { if (e.name === 'WalletSignTransactionError') { - return new TransactionError( - LiFiErrorCode.SignatureRejected, - e.message, - undefined, - e - ) + return new TransactionError(LiFiErrorCode.SignatureRejected, e.message, e) } if (e.name === 'SendTransactionError') { - return new TransactionError( - LiFiErrorCode.TransactionFailed, - e.message, - undefined, - e - ) + return new TransactionError(LiFiErrorCode.TransactionFailed, e.message, e) } if (e.message?.includes('simulate')) { return new TransactionError( LiFiErrorCode.TransactionSimulationFailed, e.message, - undefined, e ) } @@ -52,5 +41,5 @@ const handleSpecificErrors = (e: any) => { return e } - return new UnknownError(e.message || ErrorMessage.UnknownError, undefined, e) + return new UnknownError(e.message || ErrorMessage.UnknownError, e) } diff --git a/src/core/checkBalance.ts b/src/core/checkBalance.ts index 96f9345d..506aaacb 100644 --- a/src/core/checkBalance.ts +++ b/src/core/checkBalance.ts @@ -40,7 +40,7 @@ export const checkBalance = async ( `start a new one with a maximum of ${current} ${token.symbol}.` } - throw new BalanceError('The balance is too low.', errorMessage) + throw new BalanceError('The balance is too low.') } } } diff --git a/src/core/stepComparison.ts b/src/core/stepComparison.ts index bdea6bb7..c035916c 100644 --- a/src/core/stepComparison.ts +++ b/src/core/stepComparison.ts @@ -42,9 +42,7 @@ export const stepComparison = async ( // The user declined the new exchange rate, so we are not going to proceed throw new TransactionError( LiFiErrorCode.ExchangeRateUpdateCanceled, - 'Exchange rate has changed!', - `Transaction was not sent, your funds are still in your wallet. - The exchange rate has changed and the previous estimation can not be fulfilled due to value loss.` + `Exchange rate has changed!\nTransaction was not sent, your funds are still in your wallet.\nThe exchange rate has changed and the previous estimation can not be fulfilled due to value loss.` ) } diff --git a/src/errors/SDKError.unit.spec.ts b/src/errors/SDKError.unit.spec.ts index eb00b2a4..535dd406 100644 --- a/src/errors/SDKError.unit.spec.ts +++ b/src/errors/SDKError.unit.spec.ts @@ -96,7 +96,6 @@ describe('SDKError', () => { ErrorName.ValidationError, LiFiErrorCode.ValidationError, 'problem validating', - undefined, e ) } diff --git a/src/errors/baseError.ts b/src/errors/baseError.ts index 12983f09..730e6cb1 100644 --- a/src/errors/baseError.ts +++ b/src/errors/baseError.ts @@ -5,21 +5,13 @@ import { getRootCause } from './utils/rootCause.js' // they can carry addition to help give more context export class BaseError extends Error { code: ErrorCode - htmlMessage?: string override cause?: Error - constructor( - name: ErrorName, - code: number, - message: string, - htmlMessage?: string, - cause?: Error - ) { + constructor(name: ErrorName, code: number, message: string, cause?: Error) { super(message) this.name = name this.code = code - this.htmlMessage = htmlMessage this.cause = cause const rootCause = getRootCause(this.cause) diff --git a/src/errors/baseError.unit.spec.ts b/src/errors/baseError.unit.spec.ts index 755ec7fb..263f64dd 100644 --- a/src/errors/baseError.unit.spec.ts +++ b/src/errors/baseError.unit.spec.ts @@ -14,7 +14,6 @@ describe('baseError', () => { ErrorName.UnknownError, LiFiErrorCode.InternalError, 'There was an error', - undefined, intermediateError ) diff --git a/src/errors/errors.ts b/src/errors/errors.ts index 82b500a5..8c137d06 100644 --- a/src/errors/errors.ts +++ b/src/errors/errors.ts @@ -2,59 +2,32 @@ import { ErrorName, LiFiErrorCode } from './constants.js' import { BaseError } from './baseError.js' export class RPCError extends BaseError { - constructor( - code: LiFiErrorCode, - message: string, - htmlMessage?: string, - cause?: Error - ) { - super(ErrorName.RPCError, code, message, htmlMessage, cause) + constructor(code: LiFiErrorCode, message: string, cause?: Error) { + super(ErrorName.RPCError, code, message, cause) } } export class ProviderError extends BaseError { - constructor( - code: LiFiErrorCode, - message: string, - htmlMessage?: string, - cause?: Error - ) { - super(ErrorName.ProviderError, code, message, htmlMessage, cause) + constructor(code: LiFiErrorCode, message: string, cause?: Error) { + super(ErrorName.ProviderError, code, message, cause) } } export class TransactionError extends BaseError { - constructor( - code: LiFiErrorCode, - message: string, - htmlMessage?: string, - cause?: Error - ) { - super(ErrorName.TransactionError, code, message, htmlMessage, cause) + constructor(code: LiFiErrorCode, message: string, cause?: Error) { + super(ErrorName.TransactionError, code, message, cause) } } export class UnknownError extends BaseError { - constructor(message: string, htmlMessage?: string, cause?: Error) { - super( - ErrorName.UnknownError, - LiFiErrorCode.InternalError, - message, - htmlMessage, - cause - ) + constructor(message: string, cause?: Error) { + super(ErrorName.UnknownError, LiFiErrorCode.InternalError, message, cause) } } export class BalanceError extends BaseError { - constructor(message: string, htmlMessage?: string, cause?: Error) { - super( - ErrorName.BalanceError, - LiFiErrorCode.BalanceError, - message, - htmlMessage, - cause - ) + constructor(message: string, cause?: Error) { + super(ErrorName.BalanceError, LiFiErrorCode.BalanceError, message, cause) } } diff --git a/src/errors/httpError.ts b/src/errors/httpError.ts index e605430e..33a61199 100644 --- a/src/errors/httpError.ts +++ b/src/errors/httpError.ts @@ -21,7 +21,7 @@ const statusCodeToErrorClassificationMap = new Map([ { type: ErrorName.SlippageError, code: LiFiErrorCode.SlippageError, - htmlMessage: ErrorMessage.SlippageError, + message: ErrorMessage.SlippageError, }, ], [500, { type: ErrorName.ServerError, code: LiFiErrorCode.InternalError }], @@ -55,17 +55,15 @@ export class HTTPError extends BaseError { url: RequestInfo | URL, options: ExtendedRequestInit ) { - const message = createInitialMessage(response) const errorClassification = getErrorClassificationFromStatusCode( response.status ) + const additionalMessage = errorClassification?.message + ? `\n${errorClassification.message}` + : '' + const message = createInitialMessage(response) + additionalMessage - super( - ErrorName.HTTPError, - errorClassification.code, - message, - errorClassification?.htmlMessage - ) + super(ErrorName.HTTPError, errorClassification.code, message) this.type = errorClassification.type this.response = response diff --git a/src/errors/httpError.unit.spec.ts b/src/errors/httpError.unit.spec.ts index c7ac5337..d5176028 100644 --- a/src/errors/httpError.unit.spec.ts +++ b/src/errors/httpError.unit.spec.ts @@ -19,7 +19,6 @@ describe('HTTPError', () => { code: LiFiErrorCode.ValidationError, jsonFunc: () => Promise.resolve(responseBody), responseBody, - htmlMessage: undefined, builtMessage: `[ValidationError] Request failed with status code 400 Bad Request responseMessage: Oops`, }, @@ -35,7 +34,6 @@ describe('HTTPError', () => { code: LiFiErrorCode.NotFound, jsonFunc: () => Promise.resolve(responseBody), responseBody, - htmlMessage: undefined, builtMessage: `[NotFoundError] Request failed with status code 404 Not Found responseMessage: Oops`, }, @@ -46,14 +44,13 @@ describe('HTTPError', () => { 409, 'Conflict', { - initialMessage: 'Request failed with status code 409 Conflict', + initialMessage: + 'Request failed with status code 409 Conflict\nThe slippage is larger than the defined threshold. Please request a new route to get a fresh quote.', type: ErrorName.SlippageError, code: LiFiErrorCode.SlippageError, jsonFunc: () => Promise.resolve(responseBody), responseBody, - htmlMessage: - 'The slippage is larger than the defined threshold. Please request a new route to get a fresh quote.', - builtMessage: `[SlippageError] Request failed with status code 409 Conflict + builtMessage: `[SlippageError] Request failed with status code 409 Conflict\nThe slippage is larger than the defined threshold. Please request a new route to get a fresh quote. responseMessage: Oops`, }, ], @@ -69,7 +66,6 @@ describe('HTTPError', () => { code: LiFiErrorCode.InternalError, jsonFunc: () => Promise.resolve(responseBody), responseBody, - htmlMessage: undefined, builtMessage: `[ServerError] Request failed with status code 500 Internal Server Error responseMessage: Oops`, }, @@ -85,7 +81,6 @@ describe('HTTPError', () => { code: LiFiErrorCode.InternalError, jsonFunc: () => Promise.resolve(responseBody), responseBody, - htmlMessage: undefined, builtMessage: `[ServerError] Request failed with an unknown error responseMessage: Oops`, }, @@ -100,7 +95,6 @@ describe('HTTPError', () => { type: ErrorName.ValidationError, code: LiFiErrorCode.ValidationError, jsonFunc: () => Promise.reject(new Error('fail')), - htmlMessage: undefined, responseBody: undefined, builtMessage: `[ValidationError] Request failed with status code 400 Bad Request`, }, @@ -123,9 +117,6 @@ describe('HTTPError', () => { expect(error.type).toEqual(expected.type) expect(error.code).toEqual(expected.code) - if (expected.htmlMessage) { - expect(error.htmlMessage).toEqual(expected.htmlMessage) - } await error.buildAdditionalDetails() diff --git a/src/errors/utils/baseErrorRootCause.unit.spec.ts b/src/errors/utils/baseErrorRootCause.unit.spec.ts index f383e53a..800d68f8 100644 --- a/src/errors/utils/baseErrorRootCause.unit.spec.ts +++ b/src/errors/utils/baseErrorRootCause.unit.spec.ts @@ -16,7 +16,6 @@ const getErrorChain = () => { ErrorName.ValidationError, LiFiErrorCode.ValidationError, 'something happened', - undefined, NonLiFiErrorChain ) ) diff --git a/src/errors/utils/rootCause.unit.spec.ts b/src/errors/utils/rootCause.unit.spec.ts index 5fed0ae9..be4c4379 100644 --- a/src/errors/utils/rootCause.unit.spec.ts +++ b/src/errors/utils/rootCause.unit.spec.ts @@ -12,7 +12,6 @@ const getErrorChain = () => { ErrorName.ValidationError, LiFiErrorCode.ValidationError, 'something happened', - undefined, NonLiFiErrorChain ) )