agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
75 lines (65 loc) • 2.39 kB
JavaScript
/**
* @file Error handling detector
* @description Detects missing error handling in UI interactions
*/
const { UI_PROBLEM_PATTERNS } = require('./uiProblemPatterns');
/**
* Detects missing error handling for user interactions
* @param {string} content - File content
* @param {string} filePath - Path to the file
* @returns {Array} Array of error handling issues
*/
function detectMissingErrorHandling(content, filePath) {
const issues = [];
const errorPronePatterns = [
{ pattern: /fetch\s*\(/g, context: 'network requests' },
{ pattern: /axios\./g, context: 'API calls' },
{ pattern: /localStorage\./g, context: 'local storage operations' },
{ pattern: /sessionStorage\./g, context: 'session storage operations' },
{ pattern: /JSON\.parse/g, context: 'JSON parsing' },
{ pattern: /\.addEventListener/g, context: 'event listeners' },
{ pattern: /new\s+FormData/g, context: 'form data handling' }
];
const errorHandlingIndicators = [
/try\s*{/g,
/catch\s*\(/g,
/\.catch\s*\(/g,
/error|Error/g,
/onerror/gi,
/onrejected/gi
];
const hasErrorHandling = errorHandlingIndicators.some(pattern => pattern.test(content));
const missingErrorHandling = [];
errorPronePatterns.forEach(({ pattern, context }) => {
const matches = content.match(pattern);
if (matches && matches.length > 0) {
missingErrorHandling.push({
context: context,
occurrences: matches.length
});
}
});
if (missingErrorHandling.length > 0 && !hasErrorHandling) {
const patternInfo = UI_PROBLEM_PATTERNS['missing_error_handling'];
issues.push({
type: 'missing_error_handling',
severity: patternInfo.severity,
category: patternInfo.category,
location: filePath,
missingContexts: missingErrorHandling,
summary: `Error-prone operations detected without proper error handling`,
recommendation: `Add try-catch blocks, error state management, and user-friendly error messages`,
effort: patternInfo.effort,
impact: patternInfo.impact,
examples: [
'try { await axios.get(url) } catch (error) { setError(error.message) }',
'const [error, setError] = useState(null)',
'{error && <ErrorMessage message={error} />}'
]
});
}
return issues;
}
module.exports = {
detectMissingErrorHandling
};