A minimal library of MongoDB error codes, names, and helper utilities. Useful for error handling, logging, and building developer tools that interact with MongoDB.
- All official MongoDB error codes and names
- Human-friendly descriptions (where available)
- Fast lookup helpers: code ↔ name, code → description
- TypeScript types for safety and autocompletion
Install with your favorite package manager:
npm install mongo-error-codes
yarn add mongo-error-codes
pnpm add mongo-error-codes
import {
getErrorName,
getErrorCode,
getErrorDescription,
isKnownErrorCode,
MongoErrorList,
} from "mongo-error-codes";
console.log(getErrorName(11000)); // "DuplicateKey"
console.log(getErrorCode("DuplicateKey")); // 11000
console.log(getErrorDescription(11000)); // description or undefined
console.log(isKnownErrorCode(11000)); // true
// List all error codes
console.log(MongoErrorList);
Returns the error name for a given code.
import { getErrorName } from "mongo-error-codes";
console.log(getErrorName(11000)); // "DuplicateKey"
Returns the error code for a given name.
import { getErrorCode } from "mongo-error-codes";
console.log(getErrorCode("DuplicateKey")); // 11000
Returns the human-friendly description for a given code, if available.
import { getErrorDescription } from "mongo-error-codes";
console.log(getErrorDescription(11000)); // e.g. "Duplicate key error collection"
Returns true
if the code is a known MongoDB error code.
import { isKnownErrorCode } from "mongo-error-codes";
console.log(isKnownErrorCode(11000)); // true
Enum of all MongoDB error codes, mapping names to their numeric values.
import { MongoErrorCodes } from "mongo-error-codes";
console.log(MongoErrorCodes.DuplicateKey); // 11000
Represents a MongoDB error code object.
export interface MongoError {
code: number;
name: string;
description?: string;
}
An array of all error code objects:
import { MongoErrorList } from "mongo-error-codes";
console.log(MongoErrorList[0]);
// { code: 1, name: "InternalError", description: "An unspecified internal error occurred." }
A map from error code to error object:
import { MongoCodeErrorMap } from "mongo-error-codes";
console.log(MongoCodeErrorMap.get(11000));
// { code: 11000, name: "DuplicateKey" }
A map from error name to error object:
import { MongoNameErrorMap } from "mongo-error-codes";
console.log(MongoNameErrorMap.get("DuplicateKey"));
// { code: 11000, name: "DuplicateKey" }
export interface MongoError {
code: number;
name: string;
description?: string;
}
- Fork the repo and create your branch.
- Add or update error codes, descriptions, or helpers.
- Run tests and build.
- Open a pull request.
MIT