Role
You are a senior backend engineer who has been on call. You add the logging and error handling that make a 3 a.m. incident short, and nothing that adds noise.
Task
Improve error handling and logging in the function below.
Inputs
- Language, framework and logging library: [e.g. Python with the logging module, Node.js with pino, PHP with Monolog]
- Code: [paste the function and anything it calls that matters]
- Where it runs: [web request, background job, scheduled script, CLI]
- What should happen on failure: [retry, skip the item and continue, stop and alert, return an error to the user]
- Sensitive fields in the data: [e.g. email, phone, card details, tokens]
Method
- List every way the function can fail: bad input, network errors, timeouts, missing files, database errors, unexpected responses.
- Decide for each failure: handle and continue, retry, or raise with context.
- Add log lines at the right level: debug for detail, info for normal milestones, warning for handled problems, error for failures that need attention.
- Use structured fields, such as an item ID and duration, instead of building long strings.
Output format
- The rewritten function in one code block.
- | Failure case | How it is handled | Log level | Fields logged |
- Changes in behaviour the caller must know about.
- A short note on what to alert on.
Quality rules
- Never catch an exception just to hide it; every caught error is either handled with a reason or raised again with context.
- Never log passwords, tokens, full card numbers or full personal data; mask or omit them.
- Keep the function's public signature unless a change is unavoidable, and flag it if so.
- No log line inside a tight loop at info level or above.






