8000 GitHub - dmltdev/mongo-error-codes: A minimal JavaScript library of MongoDB error codes, names, and helper utilities
[go: up one dir, main page]

Skip to content

A minimal JavaScript library of MongoDB error codes, names, and helper utilities

License

Notifications You must be signed in to change notification settings

dmltdev/mongo-error-codes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Error Codes Library

A minimal library of MongoDB error codes, names, and helper utilities. Useful for error handling, logging, and building developer tools that interact with MongoDB.

Features

  • 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

Installation

Install with your favorite package manager:

npm install mongo-error-codes
yarn add mongo-error-codes
pnpm add mongo-error-codes

Usage

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);

API Reference

Functions

getErrorName(code: number): string | undefined

Returns the error name for a given code.

import { getErrorName } from "mongo-error-codes";
console.log(getErrorName(11000)); // "DuplicateKey"

getErrorCode(name: string): number | undefined

Returns the error code for a given name.

import { getErrorCode } from "mongo-error-codes";
console.log(getErrorCode("DuplicateKey")); // 11000

getErrorDescription(code: number): string | undefined

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"

isKnownErrorCode(code: number): boolean

Returns true if the code is a known MongoDB error code.

import { isKnownErrorCode } from "mongo-error-codes";
console.log(isKnownErrorCode(11000)); // true

Data Structures

MongoErrorCodes (enum)

Enum of all MongoDB error codes, mapping names to their numeric values.

import { MongoErrorCodes } from "mongo-error-codes";
console.log(MongoErrorCodes.DuplicateKey); // 11000

MongoError (interface)

Represents a MongoDB error code object.

export interface MongoError {
  code: number;
  name: string;
  description?: string;
}

MongoErrorList: MongoError[]

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." }

MongoCodeErrorMap: Map<number, MongoError>

A map from error code to error object:

import { MongoCodeErrorMap } from "mongo-error-codes";
console.log(MongoCodeErrorMap.get(11000));
// { code: 11000, name: "DuplicateKey" }

MongoNameErrorMap: Map<string, MongoError>

A map from error name to error object:

import { MongoNameErrorMap } from "mongo-error-codes";
console.log(MongoNameErrorMap.get("DuplicateKey"));
// { code: 11000, name: "DuplicateKey" }

Types

export interface MongoError {
  code: number;
  name: string;
  description?: string;
}

Contributing

  1. Fork the repo and create your branch.
  2. Add or update error codes, descriptions, or helpers.
  3. Run tests and build.
  4. Open a pull request.

License

MIT

About

A minimal JavaScript library of MongoDB error codes, names, and helper utilities

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0