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.
- Basic integration
- Supported errors
- Configuration
- Strict error detection
- Example
- 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:
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:
| Error | HTTP Status | Production Behavior |
|---|---|---|
Duplicate key (11000, 11001) | 409 Conflict | Duplicate field value entered |
ValidationError | 400 Bad Request | Returns Mongoose validation messages |
CastError | 400 Bad Request | Invalid 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:
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:
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:
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:
{
"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
ds-express-errors