Fix: Implement global try/catch to log all errors

This commit is contained in:
2025-09-26 07:34:46 -07:00
parent 7c704655aa
commit f9ac5b2e0d

View File

@@ -44,34 +44,31 @@ function generateMarkdownReport(results) {
return report; return report;
} }
function generateBuildFailureReport(error) { function generateCrashReport(error, step) {
let report = `# Hi Language Test Results\n\n`; let report = `# Hi Language Test Results\n\n`;
report += `**Run at:** ${new Date().toISOString()}\n\n`; report += `**Run at:** ${new Date().toISOString()}\n\n`;
report += `| Test Case | Status |\n`; report += `| Test Case | Status |\n`;
report += `|-----------|--------|\n`; report += `|-----------|--------|\n`;
report += `| Build Step | ❌ FAIL |\n`; report += `| ${step} | ❌ FAIL |\n`;
report += `\n---\n\n## Failures\n\n`; report += `\n---\n\n## Failures\n\n`;
report += `### \`Build Step\`\n\n`; report += `### \`${step}\`\n\n`;
report += `**Reason:** Master, failed to build parser from \`grammar.ne\`.\n\n`; report += `**Reason:** Master, the test runner encountered a fatal error.\n\n`;
report += `**Error:**\n\`\`\`\n${error.stderr || error.message}\n\`\`\`\n\n`; report += `**Error:**\n\`\`\`\n${error.stack || error.message}\n\`\`\`\n\n`;
report += `---\n\n`; report += `---\n\n`;
return report; return report;
} }
async function run() { async function run() {
try {
// 1. Build Step
try { try {
console.log("Building parser from grammar, Master..."); console.log("Building parser from grammar, Master...");
execSync('npm run build-parser'); execSync('npm run build-parser');
} catch (buildError) { } catch (buildError) {
console.error('Master, the parser build failed.'); throw { step: 'Build Step', error: buildError.stderr || buildError.message };
const report = generateBuildFailureReport(buildError);
writeFileSync(resultsFile, report);
console.log(`Failure report written to ${resultsFile}`);
process.exit(1);
return;
} }
// Build succeeded, now we can safely import and run tests. // 2. Transpiler Import & Test Execution
const { hi2js } = await import('./transpiler.js'); const { hi2js } = await import('./transpiler.js');
const testFiles = readdirSync(srcDir).filter(file => file.endsWith('.hi')); const testFiles = readdirSync(srcDir).filter(file => file.endsWith('.hi'));
@@ -129,6 +126,17 @@ async function run() {
} else { } else {
console.log("All tests passed, Master."); console.log("All tests passed, Master.");
} }
} catch (e) {
// This is the global catch block for any fatal error.
const step = e.step || 'Initialization Step';
const error = e.error || e;
console.error(`Master, a fatal error occurred during the ${step}.`);
console.error(error);
const report = generateCrashReport(error, step);
writeFileSync(resultsFile, report);
console.log(`Failure report written to ${resultsFile}`);
process.exit(1);
}
} }
run(); run();