Skip to content

Error Codes

Every error thrown by migronaut extends MigronautError and carries a typed code, a message, and an optional context object. Catch MigronautError and switch on code for precise handling:

ts
import { MigronautError } from '@alexify/migronaut';

try {
  await migrator.up();
} catch (err) {
  if (err instanceof MigronautError) {
    console.error(err.code, '—', err.message, err.context);
  }
}

Every error also carries cause — the original Error, with its stack — when it wraps one, so err.cause.stack still points into your own migration. Pass --verbose to have the CLI print it.

In --json mode the CLI prints:

json
{
  "error": { "code": "MIGRATION_EXECUTION_FAILED", "message": "…", "context": { "…": "…" } },
  "partial": [{ "file": "0001-a.ts", "status": "applied", "duration": 12, "batch": 3 }]
}

partial lists what already succeeded before the failure, so a deploy pipeline can tell how far the run got. The exit code identifies the failure — see Exit codes.

Reference

CodeError classWhen it's thrownWhat to do
LOCK_ALREADY_HELDLockAlreadyHeldErrorAnother run holds the lock within its TTLWait, or migronaut unlock if it's stale
LOCK_RELEASE_FAILEDLockReleaseFailedErrorThe lock couldn't be releasedCheck DB connectivity; retry
LOCK_LOSTLockLostErrorThe lock was lost mid-run (reclaimed, or the heartbeat couldn't reach the DB)Check what else is migrating; re-run up once it's clear
RUN_ABORTEDRunAbortedErrorThe run was stopped by stop() or SIGINT/SIGTERMSee context.results for what was applied, then re-run
HOOK_FAILEDHookFailedErrorOne of your lifecycle hooks threwcontext.hook names it; context.cause has the message
CHECKSUM_MISMATCHChecksumMismatchErrorAn applied file was edited (in --strict)Don't edit applied files — write a new migration
MIGRATION_FILE_NOT_FOUNDMigrationFileNotFoundErrorA named migration file doesn't existCheck the filename and migrationsDir
MIGRATION_FILE_EXISTSMigrationFileExistsErrormigronaut create would overwrite an existing filePick a different name, or delete the existing file
MIGRATION_INVALID_NAMEMigrationInvalidNameErrorA migration name escapes the migrations dir, or isn't a stringUse a bare filename, not a path
MIGRATION_INVALID_EXPORTMigrationInvalidExportErrorA file is missing up/down functionsExport both up and down
MIGRATION_EXECUTION_FAILEDMigrationExecutionFailedErrorA migration's up/down threwRead the cause; fix the migration logic
MIGRATION_TIMEOUTMigrationTimeoutErrorA migration ran longer than timeoutMsRaise the limit, or make the migration watch ctx.signal
TRANSACTIONS_UNSUPPORTEDTransactionsUnsupportedErroruseTransaction is on, but the deployment is standaloneSet useTransaction: false, or run a replica set / mongos
CONFIG_INVALIDConfigInvalidErrorConfig failed validationCheck required fields and types
CONFIG_FILE_EXISTSConfigFileExistsErrormigronaut init found an existing configUse --force to overwrite
CONNECTION_FAILEDConnectionFailedErrorCouldn't connect to MongoDBVerify uri/dbName and that Mongo is up
NOT_APPLIEDNotAppliedErrorTried to revert a migration that isn't appliedRun migronaut status to see what's applied
IMPORT_TARGET_NOT_EMPTYImportTargetNotEmptyErrormigronaut import target already has recordsUse --force to import anyway
MIGRATION_IRREVERSIBLEIrreversibleMigrationErrorTried to revert an imported migrate-mongo recordWrite a new forward migration instead

See Troubleshooting for step-by-step fixes for the most common ones.

Released under the MIT License.