Skip to content
GitHub

UserError

class in meocord/common Since 4.1.0

class UserError extends Error

A mistake the user can fix, such as too few coins, rather than a fault in the bot.

Throw it from a handler, a pipe, a service or a guard to tell the user what to change; for a fault in the bot, throw any other error. The built-in fallback shows its message to the user who made the call, privately for an interaction and as a reply to a message.

Examples

TypeScript
function charge(balance: number, price: number): number {
  if (balance < price) {
    throw new UserError(`You need ${price - balance} more coins.`, { code: 'shop.poor', context: { missing: price - balance } })
  }
  return balance - price
}

Members

constructor

new UserError(message: string, options?: UserErrorOptions)

Parameters

NameTypeDescription
messagestring

What the user is told.

options?UserErrorOptions

A code, the context the message is built from, and the cause.

options.code?string

Names the error, for a filter or presenter to branch on, or to look a translation up by.

options.context?Readonly<Record<string, unknown>>

Values the message is built from, such as the amount missing, for a translation to fill in.

options.cause?unknown

The error that led to this one, such as a lookup that failed.

code

readonly code?: string

Names the error, for a filter or presenter to branch on.

context

readonly context?: Readonly<Record<string, unknown>>

Values the message is built from.