Skip to content

Translate spend errors from lib to InsufficientFundsError#95

Merged
samholmes merged 1 commit intomasterfrom
sam/insufficient-funds-error
Jul 3, 2025
Merged

Translate spend errors from lib to InsufficientFundsError#95
samholmes merged 1 commit intomasterfrom
sam/insufficient-funds-error

Conversation

@samholmes
Copy link
Copy Markdown
Collaborator

@samholmes samholmes commented Jun 30, 2025

CHANGELOG

Does this branch warrant an entry to the CHANGELOG?

  • Yes
  • No

Dependencies

none

Description

none

options
)
} catch (e: any) {
} catch (error: any) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be catch (error: unknown), and then use if (error instanceof Error) prior to accessing error.message. Since the error instanceof Error check takes us back into type-land, we can also do error.message === 'Not enough spendables' instead of using cleaners - that's a valid property access at that point. Something more like this:

try {
} catch (error: unknown) {
  if (!(error instanceof error)) {
    throw new Error(String(error))
  }

  if (error.message === 'Not enough spendables') {
    throw new InsufficientFundsError({
        tokenId: PRIMARY_CURRENCY_TOKEN_ID
      })
  }

   const regex = / Have (\d*\.?\d+) XMR; need (\d*\.?\d+) XMR./gm
   const subst = `\nHave: $1 XMR.\nNeed: $2 XMR.`
   const msgFormatted = error.message.replace(regex, subst)
   throw new Error(msgFormatted)
 }

Also, isn't the "Have: $1 XMR.\nNeed: $2 XMR" error another case of insufficient funds?

@samholmes samholmes force-pushed the sam/insufficient-funds-error branch from b625990 to 4707f8f Compare July 3, 2025 18:19
@samholmes samholmes merged commit 0c9d178 into master Jul 3, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants