Skip to main content

@rtorcato/api-errors-express v1.0.0

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.

Parameters

fn

(req, res, next) => unknown

Returns

RequestHandler

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)
}))

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