0xtrails
Version:
SDK for Trails
140 lines (123 loc) • 4.54 kB
text/typescript
export function getFullErrorMessage(err: any) {
const messages = []
let current = err
while (current) {
if (typeof current === "string") {
messages.push(current)
}
if (typeof current.message === "string") {
messages.push(current.message)
}
// Check for nested error cause
current = current.cause ?? current.originalError ?? current.error ?? null
}
return messages.join(" | Caused by: ")
}
export function getErrorString(err: unknown): string {
return err instanceof Error ? err.message : (err?.toString() ?? "")
}
export function getIsWalletRejectedError(err: unknown) {
const isRejected = /rejected|denied/gi.test(getErrorString(err).toLowerCase())
return isRejected
}
export function getIsBalanceTooLowError(err: unknown) {
const isBalanceTooLow =
/have enough balance|not enough balance|insufficient balance|insufficient funds|not enough funds|too low/gi.test(
getErrorString(err).toLowerCase(),
)
return isBalanceTooLow
}
export class InsufficientBalanceError extends Error {
constructor(message: string) {
super(message)
this.name = "InsufficientBalanceError"
}
}
export function getIsApiError(err: unknown) {
const isApiError = /request aborted|not deployed/gi.test(
getErrorString(err).toLowerCase(),
)
return isApiError
}
export function getIsRateLimitedError(err: unknown) {
const isRateLimited = /slow down|request rate|rate limit/gi.test(
getErrorString(err).toLowerCase(),
)
return isRateLimited
}
export function getIsRequiredAmountNotMetError(err: unknown) {
// Example error:
// GetIntentsQuote failed: total required amount (196159816026182793237) exceeds user-specified maximum (21960919303474978846)
const isRequiredAmountNotMet = /exceeds user-specified maximum/gi.test(
getErrorString(err).toLowerCase(),
)
return isRequiredAmountNotMet
}
export function getIsNoAvailableQuoteError(err: unknown) {
const isNoAvailableQuote =
/no available quotes|failed to get quote from any provider/gi.test(
getErrorString(err).toLowerCase(),
)
return isNoAvailableQuote
}
export function getIsQuoteFailedError(err: unknown) {
const isQuoteFailed = /GetIntentsQuote failed/gi.test(
getErrorString(err).toLowerCase(),
)
return isQuoteFailed
}
export function getIsQuoteTokenError(err: unknown) {
const isQuoteTokenError =
/requires destination token to be USDC|Token or chain not found/gi.test(
getErrorString(err).toLowerCase(),
)
return isQuoteTokenError
}
export function getIsQuoteInputError(err: unknown) {
const isQuoteInputError =
/Invalid input or output currency|originTokenAmount must be greater than zero|trails fee is greater than the origin token amount/gi.test(
getErrorString(err).toLowerCase(),
)
return isQuoteInputError
}
export function getIsInsufficientLiquidityError(err: unknown) {
const isInsufficientLiquidity =
/insufficient liquidity|higher than the available liquidity/gi.test(
getErrorString(err).toLowerCase(),
)
return isInsufficientLiquidity
}
export function getPrettifiedErrorMessage(
err: unknown,
defaultMessage: string = "",
) {
const isRequiredAmountNotMet = getIsRequiredAmountNotMetError(err)
if (isRequiredAmountNotMet) {
return "The requested amount is too low to retrieve a quote or the account has insufficient funds to meet the required amount. Try again with a higher amount or make sure you have enough funds."
}
const isInsufficientLiquidity = getIsInsufficientLiquidityError(err)
if (isInsufficientLiquidity) {
return "The requested amount is too high to retrieve a quote. Try again with a lower amount."
}
const isNoAvailableQuote = getIsNoAvailableQuoteError(err)
if (isNoAvailableQuote) {
return "No quote found for the selected pair and/or amount. Try again with a different pair and/or amount."
}
const isQuoteTokenError = getIsQuoteTokenError(err)
if (isQuoteTokenError) {
return "The selected token is not supported. Please try again with a different token."
}
const isQuoteInputError = getIsQuoteInputError(err)
if (isQuoteInputError) {
return "No quote found for the input amount. Try again with a higher amount."
}
const isQuoteFailed = getIsQuoteFailedError(err)
if (isQuoteFailed) {
return "No quote found for the selected pair and amount."
}
const isApiError = getIsApiError(err)
if (isApiError) {
return "An API error occurred. Please try again later."
}
return defaultMessage
}