@ordojs/dev-tools
Version:
Advanced developer tools for OrdoJS with component inspector, AST explorer, and performance profiling
216 lines (214 loc) • 5.83 kB
JavaScript
import { EventEmitter } from 'events';
// src/error-overlay/index.ts
var ErrorOverlay = class extends EventEmitter {
errors;
isRunning;
port;
/**
* Create a new ErrorOverlay instance
*
* @param port - WebSocket port for error overlay
*/
constructor(port = 24683) {
super();
this.errors = /* @__PURE__ */ new Map();
this.isRunning = false;
this.port = port;
}
/**
* Start the error overlay
*/
async start() {
if (this.isRunning) {
console.warn("Error overlay is already running");
return;
}
try {
await this.startWebSocketServer();
this.isRunning = true;
console.log(`Error overlay started on port ${this.port}`);
this.emit("started");
} catch (error) {
console.error("Failed to start error overlay:", error);
this.emit("error", error);
throw error;
}
}
/**
* Stop the error overlay
*/
async stop() {
if (!this.isRunning) {
console.warn("Error overlay is not running");
return;
}
try {
await this.stopWebSocketServer();
this.isRunning = false;
console.log("Error overlay stopped");
this.emit("stopped");
} catch (error) {
console.error("Failed to stop error overlay:", error);
this.emit("error", error);
throw error;
}
}
/**
* Add an error to the overlay
*
* @param errorId - Unique error identifier
* @param errorInfo - Error information
*/
addError(errorId, errorInfo) {
this.errors.set(errorId, errorInfo);
this.emit("errorAdded", { errorId, errorInfo });
}
/**
* Update an error
*
* @param errorId - Error identifier
* @param updates - Error updates
*/
updateError(errorId, updates) {
const error = this.errors.get(errorId);
if (error) {
const updatedError = { ...error, ...updates };
this.errors.set(errorId, updatedError);
this.emit("errorUpdated", { errorId, errorInfo: updatedError });
}
}
/**
* Remove an error
*
* @param errorId - Error identifier
*/
removeError(errorId) {
const error = this.errors.get(errorId);
if (error) {
this.errors.delete(errorId);
this.emit("errorRemoved", { errorId, errorInfo: error });
}
}
/**
* Get error by ID
*
* @param errorId - Error identifier
* @returns Error information or undefined
*/
getError(errorId) {
return this.errors.get(errorId);
}
/**
* Get all errors
*
* @returns Array of all errors
*/
getAllErrors() {
return Array.from(this.errors.values());
}
/**
* Clear all errors
*/
clearErrors() {
this.errors.clear();
this.emit("errorsCleared");
}
/**
* Get error statistics
*
* @returns Error statistics
*/
getErrorStats() {
const errors = Array.from(this.errors.values());
const errorsByType = {};
const errorsByFile = {};
for (const error of errors) {
const errorType = this.getErrorType(error);
errorsByType[errorType] = (errorsByType[errorType] || 0) + 1;
if (error.filePath) {
errorsByFile[error.filePath] = (errorsByFile[error.filePath] || 0) + 1;
}
}
return {
totalErrors: errors.length,
errorsByType,
errorsByFile
};
}
/**
* Generate error suggestions
*
* @param errorId - Error identifier
* @returns Array of suggestions
*/
generateSuggestions(errorId) {
const error = this.errors.get(errorId);
if (!error) {
return [];
}
const suggestions = [];
if (error.message.includes("Cannot find module")) {
suggestions.push("Check if the module is installed and imported correctly");
suggestions.push("Verify the module path is correct");
suggestions.push("Try running npm install or pnpm install");
}
if (error.message.includes("Unexpected token")) {
suggestions.push("Check for syntax errors in your code");
suggestions.push("Verify all brackets, parentheses, and quotes are properly closed");
suggestions.push("Check for missing semicolons or commas");
}
if (error.message.includes("TypeError")) {
suggestions.push("Check if the variable or property exists");
suggestions.push("Verify the data type is correct");
suggestions.push("Add proper null/undefined checks");
}
if (error.message.includes("ReferenceError")) {
suggestions.push("Check if the variable is declared before use");
suggestions.push("Verify the variable name is spelled correctly");
suggestions.push("Check the scope of the variable");
}
suggestions.push("Check the browser console for more details");
suggestions.push("Try refreshing the page");
suggestions.push("Check the documentation for the correct usage");
return suggestions;
}
/**
* Get error type from error message
*
* @param error - Error information
* @returns Error type
*/
getErrorType(error) {
if (error.message.includes("Cannot find module")) {
return "ModuleNotFound";
}
if (error.message.includes("Unexpected token")) {
return "SyntaxError";
}
if (error.message.includes("TypeError")) {
return "TypeError";
}
if (error.message.includes("ReferenceError")) {
return "ReferenceError";
}
if (error.message.includes("RangeError")) {
return "RangeError";
}
return "Unknown";
}
/**
* Start WebSocket server for error overlay communication
*/
async startWebSocketServer() {
console.log("Starting WebSocket server for error overlay...");
}
/**
* Stop WebSocket server
*/
async stopWebSocketServer() {
console.log("Stopping WebSocket server for error overlay...");
}
};
export { ErrorOverlay };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map