Always implement comprehensive error handling for asynchronous operations, external API calls, and database operations. Catch errors at appropriate levels, log them with sufficient context for debugging, and provide meaningful error responses or recovery mechanisms.
Always implement comprehensive error handling for asynchronous operations, external API calls, and database operations. Catch errors at appropriate levels, log them with sufficient context for debugging, and provide meaningful error responses or recovery mechanisms.
Key principles:
Example:
async function processUserData(userId: string) {
try {
// Attempt primary operation
const result = await database.users.findUnique({
where: { id: userId },
});
if (!result) {
logger.warn("User not found", { userId });
return { error: "User not found" };
}
try {
// Attempt secondary operation
await externalApi.process(result);
return { success: true };
} catch (error) {
// Handle specific operation failure
logger.error("API processing failed", {
userId,
error: error instanceof Error ? error.message : String(error),
});
// Attempt fallback or recovery
return { error: "Processing failed", retry: true };
}
} catch (error) {
// Handle critical failures
logger.error("Critical database error", {
userId,
error: error instanceof Error ? error.stack : String(error),
});
throw new Error("Internal server error");
}
}
This approach ensures:
Enter the URL of a public GitHub repository