Skip to main content
Version: 0.14.0 (Latest)

๐Ÿšฆ Middleware

The SDK currently exposes one middleware from the middlewares namespace: nodeBlocksErrorMiddleware. It converts errors passed to Express into the Nodeblocks error response format.

import express from 'express';
import {middlewares, services} from '@nodeblocks/backend-sdk';

const {nodeBlocksErrorMiddleware} = middlewares;
const {identitiesService} = services;

const app = express();
app.use('/api/identities', identitiesService(dataStores, configuration));
app.use(nodeBlocksErrorMiddleware());

Register it after service routers. defService calls Express next(error) when validation or a route handler fails; it does not send the final error response itself.

Response formatโ€‹

nodeBlocksErrorMiddleware() reads statusCode, message, and optional data from a NodeblocksError and responds with:

{
"error": {
"message": "Validation Error",
"data": []
}
}

The status defaults to 500 when statusCode is absent. In development, the response also includes error.stack. The middleware logs the error with utils.nodeblocksLogger.

For error construction and orThrow mappings, see Error Handling ยป.