===== ISTEM =====
Our nightly export job crashed. Here is the log:

```
2026-09-18 02:00:01 INFO  export: starting nightly export (batch=500)
2026-09-18 02:00:03 INFO  export: fetched 1500 rows from orders
2026-09-18 02:00:03 INFO  export: writing batch 1/3
2026-09-18 02:00:04 INFO  export: writing batch 2/3
2026-09-18 02:00:04 ERROR export: unhandled exception
Traceback (most recent call last):
  File "/srv/jobs/export.py", line 88, in run
    write_batch(rows[i:i + BATCH], out)
  File "/srv/jobs/export.py", line 41, in write_batch
    out.write(f"{r['id']},{r['total']:.2f},{r['currency']}\n")
KeyError: 'currency'
```

What is the root cause, and at which line of our code does it fail?

===== HAM YANIT =====
### Root Cause
The root cause is a **`KeyError`** occurring because the dictionary object `r` (representing a row from the `orders` dataset) does not contain the key `'currency'`. 

When the code attempts to access `r['currency']` to format the string, the Python interpreter cannot find that key in the dictionary, causing the process to crash. This implies that at least one row in the batch being processed is missing the `currency` field (or the field is named differently in the database/source).

### Failure Location
The error occurs at **line 41** of `/srv/jobs/export.py`:

```python
out.write(f"{r['id']},{r['total']:.2f},{r['currency']}\n")
```

### Recommendations for fixing:
1.  **Check Data Integrity:** Investigate your `orders` table to see if there are rows where the `currency` column is `NULL` or if the export query is failing to select that column for certain records.
2.  **Add Defensive Coding:** Use the `.get()` method to provide a default value, or wrap the access in a `try-except` block to log the problematic row without crashing the entire job:
    *   *Example using `.get()`:* `r.get('currency', 'N/A')`
3.  **Validate Schema:** Ensure the SQL query used to fetch the 1500 rows explicitly selects the `currency` column and that no conditional logic in the query is omitting it for specific rows.