Utility Functions & Responses
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Bridge AI backend utilizes a standardized set of utility functions and response classes to ensure consistency, robust error handling, and streamlined asynchronous operations across the application. These utilities act as a middle layer between the core business logic in controllers and the Express.js framework, providing a uniform structure for API outputs and internal process management.
The primary focus of these utilities is to simplify the management of asynchronous middleware and provide a predictable interface for successful data transmission and error reporting to the client.
Request Handling & Asynchronous Operations
Async Handler Pattern
To prevent repetitive try-catch blocks and ensure that unhandled promise rejections are correctly passed to the Express error-handling middleware, the project implements an asynchandler wrapper. This utility takes a request handler function and ensures that any errors occurring during its execution are caught and forwarded using the next() function.
The flow above illustrates how the asynchandler provides a safety net for asynchronous controller logic.
Sources: BACKEND/src/utils/asynchandler.js:1-13
Controller Integration
Controllers such as chatbot.controller.js and user.controller.js rely extensively on the asynchandler to wrap their exported methods, such as processQuery, registerUser, and loginUser. This ensures that any failure in AI model invocation or database queries is handled gracefully.
| Function | Controller | Purpose |
|---|---|---|
processQuery | chatbot.controller.js | Handles AI-driven networking queries and profile extraction. |
registerUser | user.controller.js | Manages user account creation and validation. |
loginUser | user.controller.js | Handles authentication and token issuance. |
Sources: BACKEND/src/controllers/chatbot.controller.js:84-85, BACKEND/src/controllers/user.controller.js:46-47
Standardized API Responses
The ApiResponse class is used to maintain a consistent JSON structure for all successful operations. By encapsulating the status code, data payload, and a descriptive message, the backend provides a predictable interface for the frontend.
ApiResponse Structure
The class typically includes the following properties:
statusCode: The HTTP status code (e.g., 200, 201).data: The actual payload being returned to the client.message: A human-readable string describing the result.success: A boolean flag (automatically true if the status code is less than 400).
Implementation Example: Chatbot Query
In the processQuery function, the ApiResponse is used to return AI-generated content along with extracted social profiles.
return res
.status(200)
.json(
new ApiResponse(
200,
{ content, profiles },
"Response generated successfully"
)
);
Sources: BACKEND/src/controllers/chatbot.controller.js:116-124
Error Management
The ApiError utility (referenced in controllers) is used to throw structured exceptions. When combined with the asynchandler, it allows controllers to terminate execution with specific HTTP status codes and error messages.
Authentication and Validation Errors
In user.controller.js, ApiError is utilized to handle various failure scenarios:
- 400 Bad Request: Used when required fields like
emailorpasswordare missing. - 401 Unauthorized: Used for invalid credentials or expired tokens.
- 404 Not Found: Used when a user cannot be located in the database.
- 409 Conflict: Used when a username or email is already taken.
Summary
The Utility Functions & Responses module provides the architectural backbone for communication between the Bridge AI server and its clients. By utilizing the asynchandler for process flow, the ApiResponse for data consistency, and ApiError for structured failure reporting, the system maintains high reliability and developer productivity. This standardized approach allows features like the AI networking assistant and User management to share a common operational framework.