Skip to main content

@rtorcato/api-errors-express v1.0.1

Type Aliases​

ErrorHandlerOptions​

ErrorHandlerOptions = object

Defined in: errorHandler.ts:5

Properties​

includeStack?​

optional includeStack?: boolean

Defined in: errorHandler.ts:6

Functions​

asyncHandler()​

asyncHandler(fn): RequestHandler

Defined in: asyncHandler.ts:15

Wraps an async Express route handler so any rejection is forwarded to next(err), letting errorHandler deal with it instead of per-route try/catch.

Example​

app.get('/users/:id', asyncHandler(async (req, res) => {
const user = await db.findUser(req.params.id)
if (!user) throw new NotFoundError()
res.json(user)
}))

Parameters​

fn​

(req, res, next) => unknown

Returns​

RequestHandler


errorHandler()​

errorHandler(options?): ErrorRequestHandler

Defined in: errorHandler.ts:9

Parameters​

options?​

ErrorHandlerOptions = {}

Returns​

ErrorRequestHandler


notFoundHandler()​

notFoundHandler(req, _res, next): void

Defined in: notFoundHandler.ts:4

Parameters​

req​

Request

_res​

Response

next​

NextFunction

Returns​

void