You are viewing v1 of 1. Go to the current version · All versions

Diagnose a bug from a stack trace

Turn an error message and stack trace into ranked root-cause hypotheses, quick checks to confirm each, and a fix.

Change note: Initial version

Prompt

Help me find the root cause of an error in a {{language}} application.

Stack: {{stack}}
What I was doing when it happened: {{trigger}}
What I have already tried: {{tried}}

Error and stack trace:
{{trace}}

Relevant code (if any):
{{code}}

Work through it like this:
1. Read the trace from the bottom up and identify the first frame that is in my own code. Explain in plain words what that line was doing when it failed.
2. Give the 3 most likely root causes, ranked by probability. For each, give the evidence in the trace or code that supports it and one quick check (a log line, a debugger watch, a command) that would confirm or rule it out.
3. For the top candidate, show the fix as a minimal code change.
4. Say what additional information would change your ranking.
5. Suggest one guard, assertion or test so this class of bug is caught earlier next time.

Do not rewrite unrelated code. If the trace is truncated or the cause cannot be determined from what I gave you, say so and tell me exactly what to send next.

Variables

  • {{language}}Languageselect

    Options: TypeScript, JavaScript, Python, Go, Java, C#, Rust, Ruby, PHP, Kotlin, Swift, C++

    Default: Python

  • {{stack}}Framework and runtimetext

    Default: Django 5, Python 3.12, PostgreSQL

    Versions help.

  • {{trigger}}What triggered ittext

    Default: Submitting the checkout form with an empty coupon code

  • {{tried}}Already triedtext

    Default: Restarting the server; clearing the cache

  • {{trace}}Error and stack tracelongrequired

    Paste the full trace, top to bottom.

  • {{code}}Relevant codelong

    The function named in the first frame is ideal.

Example output

**Where it failed:** the first frame in your code is `apply_coupon` (checkout/services.py:48). It called `.upper()` on `None`, which raised `AttributeError`.

**Likely causes**
1. (High) An empty coupon field arrives as `None`, not an empty string, because the serializer has `allow_null=True`. Evidence: the trace shows the form POST and your note about the empty code. Check: log `repr(code)` at line 47.
2. (Medium) A mobile client sends `"coupon": null` explicitly while the web form omits the key. Check: compare request payloads in the access log.
3. (Low) A middleware strips empty strings and converts them to `None`. Check: print the raw `request.body`.

**Fix**
```python
def apply_coupon(cart, code):
    if not code:
        return cart
    code = code.strip().upper()
```

**What would change my ranking:** the serializer definition and one raw failing request.

**Prevention:** add a test that posts `coupon: null`, `""` and a missing key to checkout and expects a 200 with no discount.

Notes

Asking for ranked hypotheses with a cheap confirming check each stops the model from jumping to one confident guess. Including what you tried avoids repeated advice.