A loop with a write call inside. If one write fails, the loop should keep going — you don't want a single broken task to block the other nine. Where does the try/except go in that loop?
Around the write call itself, not around the whole loop? So a failure on one item just skips it and moves on?
Exactly. The try/except lives inside the loop body, wrapping only the risky write. The loop continues to the next item on failure:
for m in messages:
title = m.get("snippet", "")
try:
toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
created.append(title)
except Exception:
continueAnd continue is optional — the except body is empty so it falls through anyway. Adding continue just makes the intent explicit?
Explicit is better than implicit when the loop keeps going is the important behaviour. The explicit version reads more clearly. Full pipeline:
def pipeline_emails_to_tasks(max_results: int) -> list:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = emails.get("messages", [])
created = []
for m in messages:
title = m.get("snippet", "")
try:
toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
created.append(title)
except Exception:
continue
return createdThe return is the list of successes — so a run of 10 emails might return 8 titles if 2 writes failed. Should the function also report the failures?
Two-list return is the next refinement — {"created": [...], "failed": [...]}. For now the single list is the minimum useful contract: the caller knows exactly what was written and can infer failures from the difference against the input count.
So a pipeline is just this shape — fetch, loop, guard, accumulate, return. Any bulk write I write from now on uses the same three lines of defence?
Fetch, guard, accumulate. The per-item try/except is the cheapest defence you can add to any loop — three lines turn a fragile pipeline into a resilient one.
TL;DR: try/except inside the loop body, not around the loop — one failure skips one item.
execute_action call| Placement | Effect |
|---|---|
| Around the loop | One failure aborts the rest |
| Inside the loop body | One failure skips one item |
Inner wins every time for bulk pipelines. Outer is a legitimate pattern only when you want the whole batch to abort on any failure.
A loop with a write call inside. If one write fails, the loop should keep going — you don't want a single broken task to block the other nine. Where does the try/except go in that loop?
Around the write call itself, not around the whole loop? So a failure on one item just skips it and moves on?
Exactly. The try/except lives inside the loop body, wrapping only the risky write. The loop continues to the next item on failure:
for m in messages:
title = m.get("snippet", "")
try:
toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
created.append(title)
except Exception:
continueAnd continue is optional — the except body is empty so it falls through anyway. Adding continue just makes the intent explicit?
Explicit is better than implicit when the loop keeps going is the important behaviour. The explicit version reads more clearly. Full pipeline:
def pipeline_emails_to_tasks(max_results: int) -> list:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = emails.get("messages", [])
created = []
for m in messages:
title = m.get("snippet", "")
try:
toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
created.append(title)
except Exception:
continue
return createdThe return is the list of successes — so a run of 10 emails might return 8 titles if 2 writes failed. Should the function also report the failures?
Two-list return is the next refinement — {"created": [...], "failed": [...]}. For now the single list is the minimum useful contract: the caller knows exactly what was written and can infer failures from the difference against the input count.
So a pipeline is just this shape — fetch, loop, guard, accumulate, return. Any bulk write I write from now on uses the same three lines of defence?
Fetch, guard, accumulate. The per-item try/except is the cheapest defence you can add to any loop — three lines turn a fragile pipeline into a resilient one.
TL;DR: try/except inside the loop body, not around the loop — one failure skips one item.
execute_action call| Placement | Effect |
|---|---|
| Around the loop | One failure aborts the rest |
| Inside the loop body | One failure skips one item |
Inner wins every time for bulk pipelines. Outer is a legitimate pattern only when you want the whole batch to abort on any failure.
Create a free account to get started. Paid plans unlock all tracks.