UNPKG

smart-error-explanator

Version:

A Node.js SDK that explains cryptic error messages using OpenAI's API.

188 lines (132 loc) โ€ข 7.08 kB
# Smart Error Explanator **Are you fed up with copy-pasting error messages into ChatGPT or searching for solutions online?** The Smart Error Explanator SDK instantly decodes cryptic error messages, providing clear explanations, suggested fixes, and relevant documentation links. By leveraging OpenAIโ€™s API, it saves developers valuable time and eliminates the frustration of manual debugging. --- ## Advantages - No more copy-pasting error messages into ChatGPT. - Real-time error debugging for faster issue resolution. - Time-saving insights and fixes, reducing manual search efforts. - Immediate access to relevant documentation links and best practices. - Streamlined debugging process for a smoother development experience. --- ## Key Benefits - **Simplicity**: Developers just need to wrap their function with `wrapFunction` for automatic error handling and SDK invocation. - **SDK Invocation Only on Errors**: The SDK is invoked only when an error occurs in the wrapped function, reducing unnecessary API calls. - **Minimal Boilerplate**: Wrapping the function requires minimal setup. Just call `wrapFunction()` with the function you want to monitor for errors. - **Automatic Context Passing**: The function code and arguments are automatically passed to the SDK on errors, so developers don't have to manage it manually. This setup ensures smooth and automatic error handling without affecting correct function execution. - **AI-Driven Console-Based Debugging and Insights**: Developers can view detailed error explanations directly in the console. - **No Need to Leave the Console**: Everything happens within the developer's console, saving time and effort. - **No Need for Try-Catch Blocks**: The SDK automatically detects errors, allowing developers to focus on coding rather than managing error handling. - **Efficient and Streamlined Workflow**: The SDK improves productivity by managing error detection and providing insights, reducing debugging time. --- ## Features - **Detailed Explanation**: Understand the root cause of the error. - **Suggested Fixes**: Learn how to resolve or avoid the issue. - **Documentation Links**: Get relevant links for further reading. --- ## Installation Install the package using npm: ```bash npm install smart-error-explanator npm install dotenv ``` --- ### Example Usage 1: Handling Errors with `explainError` SDK Function ```javascript // Import the SmartErrorExplanator SDK import { explainError, setApiKey } from "smart-error-explanator"; import { config } from "dotenv"; // Load environment variables from a .env file config(); // Set the API key for the SmartErrorExplanator SDK setApiKey(process.env.OPENAI_API_KEY); (async () => { try { const myObject = null; // null cannot have properties or methods // Attempting to access a property on a null object (this will throw a TypeError) console.log(myObject.name); } catch (err) { // Pass the error message to the SmartErrorExplanator SDK for AI-driven debugging insights await explainError(err); } })(); ``` In this example: - A `TypeError` is triggered by trying to access a property on a `null` object. - The `explainError` function from the SDK provides **AI-driven debugging insights**, helping developers understand the issue and how to resolve it. --- ### Example Usage 2: Wrapping Functions with Arguments for AI-Driven Error Insights ```javascript // Import the SmartErrorExplanator SDK import { explainError, setApiKey, wrapFunction } from "smart-error-explanator"; import { config } from "dotenv"; // Load environment variables from a .env file config(); // Set the API key for OpenAI setApiKey(process.env.OPENAI_API_KEY); // Define a buggy function that will trigger an error const buggyFunction = async (user) => { // Simulating an error if the user's address is undefined console.log("User details:", user.name); console.log("City:", user.address.city); // Error if address is undefined }; // Wrap the function to handle errors and provide explanations const safeFunction = wrapFunction(buggyFunction); // Define test input const userInput = { name: "Alice", // 'address' is intentionally missing to cause an error }; // Execute the wrapped function with input arguments safeFunction(userInput); // This triggers the error handler and provides the explanation ``` --- ### Example Usage 3: Wrapping Functions for AI-Driven Error Insights ```javascript // Import the SmartErrorExplanator SDK import { explainError, setApiKey, wrapFunction } from "smart-error-explanator"; import { config } from "dotenv"; // Load environment variables from a .env file config(); // Set the API key for OpenAI setApiKey(process.env.OPENAI_API_KEY); // Define a buggy function that will trigger an error (no need for try-catch block) const buggyFunction = async () => { // This will cause an error since `b` is not defined console.log(b); }; // Wrap the function to handle errors and provide AI-driven explanations const safeFunction = wrapFunction(buggyFunction); // Execute the wrapped function safeFunction(); // This triggers the error handler and provides the AI-driven debugging insights ``` In this example: - **The `wrapFunction` automatically handles the error** when one occurs, eliminating the need for manual `try-catch` blocks. - The SDK catches errors within the wrapped function and provides **AI-driven insights**, explaining the issue and offering possible fixes. --- ### Sample Output ```javascript โœ” SUCCESS: API key has been successfully set. ๐Ÿ” Error in Function: async () => { // This will cause an error since `b` is not defined console.log(b); } ๐Ÿ— EXPLANATION: 1. The error "b is not defined" means that the variable `b` is being referenced in the function but it has not been declared or defined anywhere in the function or in its surrounding scope. This could be an unintentional mistake where the variable `b` was supposed to be defined before being used in the function, or it could be a typo in the code where the wrong variable name is being referenced. 2. Here are some relevant documentation links for more information: - JavaScript variables: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Variables - JavaScript scope: https://developer.mozilla.org/en-US/docs/Glossary/Scope ๐Ÿ’ก SUGGESTED FIX: Corrected Code Snippet: ```javascript async () => { let b; console.log(b); } ``` Best practices to avoid such errors in the future: 1. Always declare variables before using them to avoid the "not defined" error. 2. Use `let` or `const` to define variables within the appropriate scope. 3. Conduct thorough testing and debugging to catch such errors early on. 4. Utilize tools like linters or IDEs that offer real-time code analysis to highlight undeclared variables. **Demo Video Link: https://youtu.be/Q5ujdS1Tr9k**