Conn object, which contains the following:
req: An extended Express’Requestobject, which includes thecontext. property for accessing request-specific data added by middlewares.res: The standard ExpressResponseobject.
Suvidha constructor takes any object that implements the Handlers interface, which is defined as follows:
Understanding the Handlers
Here’s a breakdown of each handler:onSchemaErr
onSchemaErr
- Handles Zod schema validation failures gracefully.
- Typically sends an appropriate error response.
- It can call
next(err)to delegate error handling to Express’ default error handler or other middleware.
- This is called when a Zod schema validation fails (e.g., when the request body, query parameters, or path parameters do not match the defined schema).
err: TheZodErrorobject containing details about the validation failure.conn: TheConnobject containing the request (conn.req) and response (conn.res) objects.next: The Express’nextfunction.
onErr
onErr
- Handle errors that occur before a response has been sent.
- Can log the error, send an error response, or pass the error to Express’s default error handler using
next(err).
- Called when an error is thrown during middleware or request handler execution before any part of the response has been sent (headers or body).
err: This can be of any type, depending on what caused the error.conn: TheConnobject containing the request and response objects.next: The Expressnextfunction.
onComplete
onComplete
- Handles the successful completion of the request handler function when a response has not already been sent.
- Typically send the successful response based on the
outputfrom the request handler.
- This is called when the request handler function executes successfully and no part of the response (headers or body) has been sent yet.
output: The value returned by the request handler function. This can be of any type.conn: TheConnobject containing the request and response objects.next: The Expressnextfunction. While available, it’s typically not needed inonComplete.
onPostResponse
onPostResponse
- Handles anything that happens after a response has been initiated (headers sent). This includes both errors and returned values.
- Log errors that occur after the response has begun.
- Handles the (usually unintentional) scenario where the request handler returns a value after sending a response.
Methods such as
res.send() and res.json(), or any other method that
sends headers or a body, are considered equivalent and indicate that the
response has been initiated.- This is called when something happens after a response has started (headers sent).
This can be:
- Errors thrown by middleware after using
conn.res.send(). - Errors thrown by the request handler after using
res.send(). - The request handler returning a value after using
res.send(). (This is primarily for bug detection).
- Errors thrown by middleware after using
outputOrErr: Either the error object (if an error occurred) or the returned value from the request handler (if the request handler returned a value after sending the response). The type will beunknown.conn: TheConnobject containing the request and response objects.next: The Expressnextfunction. While available, it’s typically not needed inonPostResponse.
- If the request handler throws an error before sending a response,
onErris called, notonPostResponse. - If middleware sends a response, the request handler will not be executed. Any errors thrown by the middleware after sending the response are caught by
onPostResponse.