Mongoose Integration (in progress)

ds-express-errors can automatically identify and map Mongoose errors into appropriate HTTP errors and responses.

The Mongoose mapper works out of the box and does not require any additional configuration.

  1. Basic integration
  2. Supported errors
  3. Configuration
  4. Strict error detection
  5. Example
  6. Resulting responses

1. Basic Integration

Use Mongoose normally in your application and add the ds-express-errors error handler after all routes.

Add errorHandler after your application routes:

javascript

import express from 'express'
import { errorHandler } from 'ds-express-errors'

const app = express();

// Your routes

// Add Error Handler at the very end
app.use(errorHandler);

If Mongoose throws a supported error, it will be automatically identified and mapped by errorHandler.


2. Supported Errors

The Mongoose mapper handles the following errors:

ErrorHTTP StatusProduction Behavior
Duplicate key (11000, 11001)409 ConflictDuplicate field value entered
ValidationError400 Bad RequestReturns Mongoose validation messages
CastError400 Bad RequestInvalid value provided


3. Configuration

By default, all available mappers are enabled.

If you want ds-express-errors to use only selected mappers, configure them with needMappers:

javascript

import { setConfig } from 'ds-express-errors'

setConfig({ needMappers: ['mongoose'] })

Note

With this configuration, the built-in Mongoose mapper is enabled while other built-in third-party mappers are not used.


4. Strict Error Detection

Strict error detection through errorClasses is currently not available for Mongoose.

Mongoose errors are identified using the library's default zero-dependency detection.

No Mongoose package or error classes need to be passed to ds-express-errors.


5. Example

Consider a Mongoose schema with a unique email and validation rules:

javascript
const userSchema = new mongoose.Schema({ 
  email: { 
    type: String, 
    required: [true, 'Email is required'], 
    unique: true 
  }, 
  age: { 
    type: Number, 
    min: [18, 'Age must be at least 18'] 
  } 
}) 
const User = mongoose.model('User', userSchema)

If an operation attempts to create a user with an email that already exists:

javascript
await User.create({ email: 'user@example.com', age: 25 })

MongoDB can throw a duplicate key error with code 11000.

ds-express-errors identifies it and maps it to 409 Conflict.

The same applies to Mongoose validation and cast errors without requiring manual checks for each error type.


6. Resulting Responses

For a supported Mongoose error, the final response follows the standard ds-express-errors response format. (configured by setConfig())

For example, a Duplicate key error in production is mapped to:

json
{ 
	"status": "fail", 
	"message": "Duplicate field value entered" 
}

Warning

url, stack, method are available in development mode, if you want to change that setting, you can redefine them by setConfig

To see how Production and Development responses differ, visit the Configuration part

Look at formatError property