Sequelize Integration (in progress)
ds-express-errors can automatically identify and map Sequelize errors into appropriate HTTP errors and responses.
The Sequelize mapper works out of the box using zero-dependency error detection. Sequelize can also be provided through errorClasses to enable strict instanceof detection.
- Basic integration
- Supported errors
- Configuration
- Strict error detection
- Example
- Resulting responses
1. Basic Integration
Use Sequelize normally in your Express application and add the ds-express-errors error handler after all 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);When a supported Sequelize error reaches errorHandler, ds-express-errors identifies it and maps it to the appropriate HTTP error.
2. Supported Errors
The Sequelize mapper handles validation, constraint, database, connection and timeout errors.
| Error | HTTP Status | Production Message |
|---|---|---|
ValidationError | 400 Bad Request | Sequelize validation messages |
ForeignKeyConstraintError | 409 Conflict | Invalid reference |
UniqueConstraintError | 409 Conflict | Resource already exists |
OptimisticLockError | 409 Conflict | Resource conflict occurred |
EmptyResultError | 404 Not Found | Resource not found |
DatabaseError | 500 Internal Server Error | Database error occurred |
ConnectionError | 503 Service Unavailable | Database connection error occurred |
TimeoutError | 504 Gateway Timeout | Database timeout |
Info
When strict error detection is enabled, additional Sequelize connection error classes can be identified directly:
| Sequelize Error | HTTP Status | Production Message |
|---|---|---|
ConnectionRefusedError | 503 Service Unavailable | Database connection error occurred |
HostNotFoundError | 503 Service Unavailable | Database connection error occurred |
HostNotReachableError | 503 Service Unavailable | Database connection error occurred |
AccessDeniedError | 503 Service Unavailable | Database connection error occurred |
ConnectionAcquireTimeoutError | 503 Service Unavailable | Database connection error occurred |
ConnectionTimedOutError | 504 Gateway Timeout | Database timeout |
Note
Development responses contain additional information from the original Sequelize error.
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: ['sequelize'] })Note
With this configuration, the built-in Sequelize mapper is enabled while other built-in third-party mappers are not used.
4. Strict Error Detection
By default, ds-express-errors follows its zero dependency policy and identifies supported third-party errors without requiring Sequelize as its own dependency.
If stricter error detection is required, Sequelize error classes can be provided through errorClasses.
This allows the library to use the supplied Sequelize classes for error identification instead of relying on the default detection mechanism.
Info
Strict detection is optional. The Sequelize mapper works without it.
import { setConfig } from 'ds-express-errors'
import Sequelize from 'sequelize'
setConfig({
errorClasses: { Sequelize }
})When Sequelize is provided, ds-express-errors uses instanceof checks against Sequelize error classes.
For example:
err instanceof Sequelize.UniqueConstraintError
err instanceof Sequelize.ValidationError
err instanceof Sequelize.DatabaseErrorWarning
Strict detection also allows the mapper to distinguish specific Sequelize connection errors such as ConnectionRefusedError, HostNotFoundError, HostNotReachableError and AccessDeniedError.
5. Example
Consider a Sequelize model with a unique email:
const User = sequelize.define('User',
{
email:
{
type: DataTypes.STRING,
allowNull: false,
unique: true
}
})Creating another user with the same email can result in a Sequelize UniqueConstraintError:
await User.create({ email: 'user@example.com' })Once the error reaches errorHandler, ds-express-errors maps it to 409 Conflict.
You do not need to manually check for UniqueConstraintError and create the corresponding HTTP error in every route.
6. Resulting Responses
For a supported Sequelize error, the final response follows the standard ds-express-errors response format. (configured by setConfig())
{
"status": "fail",
"message": "Resource already exists"
}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