Skip to content
GitHub

isExplainedError

function in meocord/common Since 4.1.0

isExplainedError(error: unknown): boolean

Whether MeoCord has already logged what went wrong and what to do about it.

Use it where a bot logs a startup failure, so an error MeoCord explained is not logged twice: an app MeoCordFactory.create() refuses, such as two handlers for one command, or Discord refusing the bot token or a privileged intent at login. A built bot has process.exitCode set for such an error already, and a start that a stop ended, which is explained too, leaves it as it was, so the process exits 0. Outside a build, as a script run by hand, MeoCordFactory.create() sets no code for an app it refuses, so a catch that returns early for an explained error, as below, sets process.exitCode itself there. The error is otherwise unchanged.

Parameters

NameType
errorunknown

An error MeoCordFactory.create() threw or app.start() rejected with.

Returns

boolean

true when MeoCord explained the error.

Examples

TypeScript
// src/main.ts, as a generated app has it
declare const App: new () => object
const logger = new Logger()

async function bootstrap() {
  const app = MeoCordFactory.create(App)
  await app.start()
}

// An error MeoCord explained, such as an app it refuses or a token Discord refused, is already logged, with the exit code set
bootstrap().catch(error => {
  if (isExplainedError(error)) return
  logger.error('Error during startup:', error)
  process.exitCode = 1
})