Skip to content

Commit 3b401bb

Browse files
authored
Updated error detection (#37)
2 parents 08e3b68 + df3deef commit 3b401bb

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

app/api/generate/route.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ const extendedComponentSchema = componentSchema.extend({
2222

2323
export async function POST(request: NextRequest) {
2424
try {
25-
console.log('🚀 API route started');
2625
const body = await request.json();
2726

2827
// Validate the request using extended schema
2928
const validationResult = extendedComponentSchema.safeParse(body);
3029

3130
if (!validationResult.success) {
32-
console.log('❌ Validation failed:', validationResult.error.format());
3331
return NextResponse.json(
3432
{ error: 'Invalid request format', details: validationResult.error.format() },
3533
{ status: 400 }
@@ -38,25 +36,13 @@ export async function POST(request: NextRequest) {
3836

3937
const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validationResult.data;
4038

41-
console.log('✅ Validation passed, API Request:', {
42-
isEdit: !!(existingFiles && editInstruction),
43-
filesCount: existingFiles?.length || 0,
44-
editInstruction: editInstruction || 'none',
45-
description: description || 'none',
46-
useBuggyCode,
47-
useFixer
48-
});
49-
5039
// Process the app request using centralized logic
5140
const filesToSandbox = await processAppRequest(description, existingFiles, editInstruction, useBuggyCode);
5241

53-
console.log('📦 Files ready for sandbox:', filesToSandbox.length);
54-
5542
let repairedFiles = filesToSandbox;
5643

5744
// Repair the generated code using Benchify's API if requested
5845
if (useFixer) {
59-
console.log('🔧 Running Benchify fixer...');
6046
try {
6147
const { data } = await benchify.fixer.run({
6248
files: filesToSandbox.map((file: { path: string; content: string }) => ({
@@ -69,31 +55,21 @@ export async function POST(request: NextRequest) {
6955
const { success, diff } = data;
7056

7157
if (success && diff) {
72-
console.log('✅ Fixer applied successfully');
7358
repairedFiles = filesToSandbox.map((file: { path: string; content: string }) => {
7459
const patchResult = applyPatch(file.content, diff);
7560
return {
7661
...file,
7762
content: typeof patchResult === 'string' ? patchResult : file.content
7863
};
7964
});
80-
} else {
81-
console.log('⚠️ Fixer ran but no fixes were applied');
8265
}
83-
} else {
84-
console.log('⚠️ Fixer returned no data');
8566
}
8667
} catch (error) {
87-
console.error('❌ Error running fixer:', error);
8868
// Continue with original files if fixer fails
8969
}
90-
} else {
91-
console.log('⏭️ Skipping fixer as requested');
9270
}
9371

94-
console.log('🏗️ Creating sandbox...');
9572
const sandboxResult = await createSandbox({ files: repairedFiles });
96-
console.log('✅ Sandbox created successfully');
9773

9874
// Return the results to the client
9975
return NextResponse.json({
@@ -106,7 +82,6 @@ export async function POST(request: NextRequest) {
10682
...(editInstruction && { editInstruction }),
10783
});
10884
} catch (error) {
109-
console.error('💥 Error in API route:', error);
11085
return NextResponse.json(
11186
{
11287
error: 'Failed to generate app',

lib/e2b.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,13 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
162162

163163
if (buildCheck.stdout && (
164164
buildCheck.stdout.includes('Unterminated string constant') ||
165+
buildCheck.stdout.includes('Unterminated string literal') ||
165166
buildCheck.stdout.includes('SyntaxError') ||
166167
buildCheck.stdout.includes('Unexpected token') ||
167-
buildCheck.stdout.includes('[plugin:vite:')
168+
buildCheck.stdout.includes('error TS') ||
169+
buildCheck.stdout.includes('[plugin:vite:') ||
170+
buildCheck.stdout.includes('Parse error') ||
171+
buildCheck.stdout.includes('Parsing error')
168172
)) {
169173
hasCompilationError = true;
170174
compilationErrorOutput = buildCheck.stdout;
@@ -208,10 +212,18 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
208212

209213
if (errorOutput) {
210214
console.log('Adding build error with message length:', errorOutput.length);
211-
buildErrors.push({
212-
type: 'build',
213-
message: errorOutput
214-
});
215+
216+
// Parse TypeScript errors for better display
217+
const parsedErrors = parseTypeScriptErrors(errorOutput);
218+
if (parsedErrors.length > 0) {
219+
buildErrors.push(...parsedErrors);
220+
} else {
221+
// Fallback to raw error output
222+
buildErrors.push({
223+
type: 'build',
224+
message: errorOutput
225+
});
226+
}
215227
}
216228
} else if (isPermissionError) {
217229
console.log('⚠️ Permission errors detected but likely non-critical (E2B sandbox issue)');
@@ -241,6 +253,40 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
241253
};
242254
}
243255

256+
function parseTypeScriptErrors(output: string): BuildError[] {
257+
const errors: BuildError[] = [];
258+
259+
// Match TypeScript error format: file(line,col): error TSxxxx: message
260+
const tsErrorRegex = /([^(]+)\((\d+),(\d+)\): error (TS\d+): (.+)/g;
261+
262+
let match;
263+
while ((match = tsErrorRegex.exec(output)) !== null) {
264+
const [, file, line, col, errorCode, message] = match;
265+
errors.push({
266+
type: 'typescript',
267+
message: `${errorCode}: ${message}`,
268+
file: file.trim(),
269+
line: parseInt(line, 10),
270+
column: parseInt(col, 10)
271+
});
272+
}
273+
274+
// If no specific TypeScript errors found, try to extract any line that looks like an error
275+
if (errors.length === 0) {
276+
const lines = output.split('\n');
277+
for (const line of lines) {
278+
if (line.includes('error') || line.includes('Error')) {
279+
errors.push({
280+
type: 'build',
281+
message: line.trim()
282+
});
283+
}
284+
}
285+
}
286+
287+
return errors;
288+
}
289+
244290
function extractNewPackages(packageJsonContent: string): string[] {
245291
try {
246292
const packageJson = JSON.parse(packageJsonContent);

0 commit comments

Comments
 (0)