Skip to content

Commit

Permalink
feat: remove htmlmessage prop from errors (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
DNR500 authored Jul 19, 2024
1 parent 0d52900 commit b62108e
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 99 deletions.
1 change: 0 additions & 1 deletion src/core/EVM/EVMStepExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ export class EVMStepExecutor extends BaseStepExecutor {
{
error: {
message: error.cause.message,
htmlMessage: error.cause.htmlMessage,
code: error.code,
},
}
Expand Down
1 change: 0 additions & 1 deletion src/core/EVM/checkAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export const checkAllowance = async (
{
error: {
message: error.cause.message,
htmlMessage: error.cause.htmlMessage,
code: error.code,
},
}
Expand Down
10 changes: 2 additions & 8 deletions src/core/EVM/parseEVMErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -52,7 +47,6 @@ const handleSpecificErrors = async (
return new TransactionError(
LiFiErrorCode.GasLimitError,
ErrorMessage.GasLimitLow,
undefined,
e
)
}
Expand All @@ -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)
}
1 change: 0 additions & 1 deletion src/core/Solana/SolanaStepExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ export class SolanaStepExecutor extends BaseStepExecutor {
{
error: {
message: error.cause.message,
htmlMessage: error.cause.htmlMessage,
code: error.code,
},
}
Expand Down
17 changes: 3 additions & 14 deletions src/core/Solana/parseSolanaErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion src/core/checkBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/core/stepComparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
)
}

Expand Down
1 change: 0 additions & 1 deletion src/errors/SDKError.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ describe('SDKError', () => {
ErrorName.ValidationError,
LiFiErrorCode.ValidationError,
'problem validating',
undefined,
e
)
}
Expand Down
10 changes: 1 addition & 9 deletions src/errors/baseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/errors/baseError.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('baseError', () => {
ErrorName.UnknownError,
LiFiErrorCode.InternalError,
'There was an error',
undefined,
intermediateError
)

Expand Down
47 changes: 10 additions & 37 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/errors/httpError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
Expand Down Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions src/errors/httpError.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
},
Expand All @@ -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`,
},
Expand All @@ -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`,
},
],
Expand All @@ -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`,
},
Expand All @@ -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`,
},
Expand All @@ -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`,
},
Expand All @@ -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()

Expand Down
1 change: 0 additions & 1 deletion src/errors/utils/baseErrorRootCause.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const getErrorChain = () => {
ErrorName.ValidationError,
LiFiErrorCode.ValidationError,
'something happened',
undefined,
NonLiFiErrorChain
)
)
Expand Down
1 change: 0 additions & 1 deletion src/errors/utils/rootCause.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const getErrorChain = () => {
ErrorName.ValidationError,
LiFiErrorCode.ValidationError,
'something happened',
undefined,
NonLiFiErrorChain
)
)
Expand Down

0 comments on commit b62108e

Please sign in to comment.