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.

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

1. Basic Integration

Use Sequelize normally in your Express application and add the ds-express-errors error handler after all 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);

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.

ErrorHTTP StatusProduction Message
ValidationError400 Bad RequestSequelize validation messages
ForeignKeyConstraintError409 ConflictInvalid reference
UniqueConstraintError409 ConflictResource already exists
OptimisticLockError409 ConflictResource conflict occurred
EmptyResultError404 Not FoundResource not found
DatabaseError500 Internal Server ErrorDatabase error occurred
ConnectionError503 Service UnavailableDatabase connection error occurred
TimeoutError504 Gateway TimeoutDatabase timeout

Info

When strict error detection is enabled, additional Sequelize connection error classes can be identified directly:


Sequelize ErrorHTTP StatusProduction Message
ConnectionRefusedError503 Service UnavailableDatabase connection error occurred
HostNotFoundError503 Service UnavailableDatabase connection error occurred
HostNotReachableError503 Service UnavailableDatabase connection error occurred
AccessDeniedError503 Service UnavailableDatabase connection error occurred
ConnectionAcquireTimeoutError503 Service UnavailableDatabase connection error occurred
ConnectionTimedOutError504 Gateway TimeoutDatabase 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:

javascript

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.


javascript

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:

javascript
err instanceof Sequelize.UniqueConstraintError 
err instanceof Sequelize.ValidationError 
err instanceof Sequelize.DatabaseError

Warning

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:

javascript
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:

javascript
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())

json
{ 
	"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