Apps Script Error Handling: The Failed Invoice Your Ledger Skips Forever
Описание и применение
В скрипте Google Apps Script для отправки счетов возникала проблема: при неявных ошибках счет не отправлялся и не повторялся, а система оповещений генерировала дубли. Внедрение леджера, который различает ошибки и сбои, ограничивает попытки до трёх и устраняет дублирование оповещений, решает проблему.
Задача
Silent failures in Apps Script jobs cause invoices to be skipped permanently, leading to missed communications and duplicate alerts.
Результат
The updated ledger logic prevents silent skips by distinguishing failures from crashes, limits retries to three attempts, and deduplicates alerts, reducing 124 alert emails during a weekend outage to a single notification.
Рабочий процесс
- 1. Define ledger sheet with columns for key, state, attempts, and error.
- 2. Use processOnce to claim a key with a lock, marking it in_progress.
- 3. Execute the invoice send function inside a try/catch.
- 4. On success, mark the key as done; on error, mark as failed or gave_up based on MAX_ATTEMPTS.
- 5. Deduplicate alerts using alertOnce with a 3600‑second window.
- 6. Run watchdog hourly to monitor job health and send alerts only once per interval.
Доказательства
One failed send becomes an invoice that is never sent.
A weekend outage sends 124 alert emails.
processOnce asks one question before it runs anything: is this key anywhere in column B?
- Дата записи: 2026-11-02T00:29:17.566Z