TipsMake

Prompt templates help diagnose complex code errors.

Prompt templates for diagnosing complex code errors emerged as an effective support tool, leveraging the power of AI to analyze, infer, and hypothesize about the cause of the error.

In software development, simple errors are usually easy to detect and fix, but complex code errors – involving multi-layered logic, module interactions, or edge cases – pose a significant challenge even for experienced programmers. Reading and tracing the code flow to find the root cause can be very time-consuming, especially when the system has grown to a large scale.

 

Therefore, prompt templates for diagnosing complex code errors were created as an effective support tool, leveraging the power of AI to analyze, infer, and hypothesize about the cause of errors. Instead of asking general questions, structured prompts guide the AI ​​to focus on critical aspects such as data flow, dependencies, system state, and potential bug locations.

 

This article will help you build 'focused' prompts, thereby not only shortening debugging time but also improving your in-depth understanding of the system. This is a crucial step in transforming AI into a true 'technical assistant' in complex software projects.

Prompt templates help diagnose complex code errors.

Hãy giúp tôi gỡ lỗi vấn đề này: Thông báo lỗi: [DÁN LỖI] Ngữ cảnh code: [DÁN CODE LIÊN QUAN] Ngôn ngữ/Framework: [ví dụ: JavaScript/Node.js] Điều tôi mong đợi: [HÀNH VI MONG ĐỢI] Điều xảy ra: [HÀNH VI THỰC TẾ] Những gì tôi đã thử: [LIỆT KÊ CÁC THỬ NGHIỆM] Cung cấp: 1. Phân tích nguyên nhân gốc rễ 2. Giải pháp từng bước 3. Tại sao nó xảy ra 4. Cách ngăn chặn nó

Best suited for: GPT-5, Claude 4 Sonnet

How to use sample prompts

  • The error message should be: Some items are out of stock
  • The code context is:
// services/orderService.js class OrderService { constructor(paymentService, inventoryService, notificationService) { this.paymentService = paymentService; this.inventoryService = inventoryService; this.notificationService = notificationService; } async processOrder(order) { try { // 1. Validate order if (!order.userId || !order.items || order.items.length === 0) { throw new Error("Invalid order data"); } // 2. Check inventory (BUG tiềm ẩn: không await Promise.all) const inventoryChecks = order.items.map(item => { return this.inventoryService.checkStock(item.productId, item.quantity); }); const allInStock = inventoryChecks.every(result => result === true); if (!allInStock) { throw new Error("Some items are out of stock"); } // 3. Calculate total (BUG: không xử lý số âm hoặc undefined price) let total = 0; for (const item of order.items) { total += item.price * item.quantity; } // 4. Apply discount (BUG: discount có thể > 100%) if (order.discount) { total = total - (total * order.discount); } // 5. Process payment (BUG: không handle timeout / retry) const paymentResult = await this.paymentService.charge( order.userId, total ); if (!paymentResult.success) { throw new Error("Payment failed"); } // 6. Deduct inventory (BUG: race condition nếu concurrent) order.items.forEach(item => { this.inventoryService.reduceStock(item.productId, item.quantity); }); // 7. Send notification (BUG: không await → có thể fail silently) this.notificationService.sendEmail(order.userId, "Order successful"); return { status: "success", total }; } catch (error) { console.error("Order processing failed:", error.message); return { status: "failed", error: error.message }; } } } module.exports = OrderService;

 

  • Language/Framework: JavaScript
  • The expected behavior is: Display an error code to indicate at which step the error occurred and to easily categorize the error.
  • What happened:
    • Returning only error.message → lacks context (doesn't know at which step the error occurred)
    • No error code → difficult to handle on the frontend.
    • No fault classification (business vs system)
    • console.error is not sufficient for production (a logger like Winston should be used instead).
  • What I tried: Refactored the entire error handling system to meet production standards (Node.js).

The sample prompt results were obtained using GPT-5.

Hãy giúp tôi gỡ lỗi vấn đề này: Thông báo lỗi: Some items are out of stock Ngữ cảnh code: // services/orderService.js class OrderService { constructor(paymentService, inventoryService, notificationService) { this.paymentService = paymentService; this.inventoryService = inventoryService; this.notificationService = notificationService; } async processOrder(order) { try { // 1. Validate order if (!order.userId || !order.items || order.items.length === 0) { throw new Error("Invalid order data"); } // 2. Check inventory (BUG tiềm ẩn: không await Promise.all) const inventoryChecks = order.items.map(item => { return this.inventoryService.checkStock(item.productId, item.quantity); }); const allInStock = inventoryChecks.every(result => result === true); if (!allInStock) { throw new Error("Some items are out of stock"); } // 3. Calculate total (BUG: không xử lý số âm hoặc undefined price) let total = 0; for (const item of order.items) { total += item.price * item.quantity; } // 4. Apply discount (BUG: discount có thể > 100%) if (order.discount) { total = total - (total * order.discount); } // 5. Process payment (BUG: không handle timeout / retry) const paymentResult = await this.paymentService.charge( order.userId, total ); if (!paymentResult.success) { throw new Error("Payment failed"); } // 6. Deduct inventory (BUG: race condition nếu concurrent) order.items.forEach(item => { this.inventoryService.reduceStock(item.productId, item.quantity); }); // 7. Send notification (BUG: không await → có thể fail silently) this.notificationService.sendEmail(order.userId, "Order successful"); return { status: "success", total }; } catch (error) { console.error("Order processing failed:", error.message); return { status: "failed", error: error.message }; } } } module.exports = OrderService; Ngôn ngữ/Framework: JavaScript Điều tôi mong đợi: Hiển thị mã lỗi để biết lỗi xảy ra ở bước nào và dễ phân loại lỗi Điều xảy ra: ❌ Chỉ trả về error.message → thiếu context (không biết lỗi xảy ra ở bước nào) ❌ Không có mã lỗi (error code) → khó xử lý phía frontend ❌ Không phân loại lỗi (business vs system) ❌ console.error không đủ cho production (nên dùng logger như Winston) Những gì tôi đã thử: Refactor toàn bộ hệ thống error handling theo chuẩn production (Node.js) Cung cấp: 1. Phân tích nguyên nhân gốc rễ 2. Giải pháp từng bước 3. Tại sao nó xảy ra 4. Cách ngăn chặn nó

Discover more

Samuel Daniel

Share by

Samuel Daniel
Update 05 April 2026