zakariaf/flutter-skills/error-handling-typed-results
Claude Code and agent skill by zakariaf/flutter-skills
- community
Checking that the SKILL.md file is still in the repository.
Enforces a typed-error spine — a hand-rolled sealed Result<T,F> plus one per-boundary sealed Failure carrying a stable code and typed params (never a localized string), returned instead of thrown; recoverable failures are values, only bugs throw. Convert-at-boundary catches narrowly with an on-clause and logs the original error+stack BEFORE returning a typed Failure; call sites switch exhaustively with no default:; the taxonomy of what the global error net (FlutterError.onError + PlatformDispatcher.onError, installed by app-startup-and-bootstrap) routes into, plus Isolate.run re-wrapping; mechanism selection (Error vs assert vs Exception vs sealed outcome) and @useResult; and a never-lose-data layer (one-transaction-per-mutation, debounced autosave drafts, optimistic soft-delete/Undo). Use when writing result.dart/failures.dart, a try/catch or sealed-switch default:, wiring bootstrap handlers, a repository/service/DAO boundary, or transactions, drafts, or soft-delete/Undo.
Install
npx vskill@latest install zakariaf/flutter-skills/error-handling-typed-resultsSKILL.md
The same SKILL.md was published earlier as zakariaf/flutter-skills/error-handling-typed-results.
Error Handling — Typed Results
Recoverable failures are typed values that flow through the layers and are switched on exhaustively; only genuine bugs and unrecoverable states throw, and those are caught once by a global net. Wrapped around both, never-lose-data — transactions, autosave drafts, soft-delete/Undo — is a first-class subsystem, not plumbing. Two tiers, no middle ground.
Read the reference for the task at hand:
references/result-failure-spine.md— theResult/Failuresource, the per-boundary taxonomy, convert-at-boundary, the global net, isolate re-wrapping, and local logging.references/mechanism-selection.md— throw vsassertvsExceptionvs sealed outcome,@useResult, and the runZonedGuarded decision.references/never-lose-data.md— one transaction per mutation, debounced autosave drafts, optimistic soft-delete / Trash / Undo behind one filter.
Run scripts/check-swallowed-catch.sh and scripts/check-softdelete-parity.sh before a PR.
Non-negotiable rules
- Model recoverable failures as values, not exceptions. Anything that fails for a runtime reason the caller must handle — DB error, file/backup I/O, notification scheduling, an expected not-found, invalid input — returns
Result<T, F extends Failure>(sealedOk/Err). Throwing across a layer for an expected failure is a review reject. - Hand-roll one zero-dependency sealed `Result` and `Failure` in a Flutter-free layer so pure logic and repositories share ONE vocabulary.
result_dartis the sanctioned drop-in if you want ready-madeflatMap/mapError/AsyncResult— adopt it wholesale or hand-roll; never mix both. - One sealed `Failure` family per boundary, each subtype carrying a stable `code` + typed params — NEVER a user-facing or localized string. A baked-in message breaks translation, RTL mirroring, and numeral rendering. Localize from the
codeat the presentation edge. - `switch` failures exhaustively with NO `default:` / `case _:`. Sealed exhaustiveness turns "added a new failure" into a compile error until every switch covers it; a
default:silently defeats the only compiler-grade safety net you have. - Convert at the boundary — log first, then return. Wrap each dangerous call once, catch narrowly with an
onclause, log the original error + stack to the local log BEFORE returning the typed failure. APlatformException/SqliteException/FormatExceptionnever leaks past its adapter into a Notifier or widget. - Never swallow.
catch (_) {}, barecatch (e)that discards type/stack, andthrow e(userethrow) are banned — CI greps for them. Never catchErrorsubtypes (StateError,AssertionError): those are bugs, let them crash in debug. - Keep pure logic total — it never throws. Every pure function returns a value for every input; uncertainty is an explicit output (an
outOfRangevariant, a clamped value). Programmer invariants useassert(stripped in release). Async/error handling lives outside pure code, at the boundary seam. - Anything *thrown* is a bug or unrecoverable state, routed to the global net — never a recoverable failure. Recoverable failures are typed
Resultvalues that never throw across a layer, so they never reach the net. The two handlers (FlutterError.onError+PlatformDispatcher.instance.onError) and their install order are owned byapp-startup-and-bootstrap; this skill owns only the taxonomy of what reaches them. TherunZonedGuarded-only-for-a-crash-SDK decision is inreferences/mechanism-selection.md. - Re-wrap `Isolate.run`/`compute` errors as `Result` at the call site. Isolate errors do not hit
FlutterError.onError; catch them where you await or they propagate opaquely. - Log locally only. A size-capped rotating file plus a user-initiated "Export diagnostics" affordance. If you ship a crash SDK, that is a deliberate choice with its own zone; otherwise no Crashlytics/Sentry/Firebase.
- Never lose hand-entered data. One
transaction(...)per multi-table mutation (all-or-nothing); persist in-progress form state to a debounceddraftstable; delete viais_deletedsoft-delete behind a single shared filter, with SnackBar Undo. Detail inreferences/never-lose-data.md.
The Result + Failure spine
Native Dart 3 sealed, zero dependencies. Pure logic and repositories return the same type.
Sealed variants are final class + const ctor + final fields. You switch on the type; you never compare instances, so do not write ==/hashCode or reach for freezed/equatable here.
Convert at the boundary
Wrap the dangerous call once. Log the original exception + stack before returning a typed, string-free failure. @useResult makes discarding the outcome a lint error.
Exhaustive switch at the call site
The Notifier (ViewModel) switches every case; localization happens here, from the code.
For UI that recovers per-case rather than showing a generic error, switch the failure in the widget and map each code to an l10n message — never a default:.
The global net routes only thrown bugs
app-startup-and-bootstrap owns the net and everything about it: the two handlers — FlutterError.onError (build/layout/paint sync errors) and PlatformDispatcher.instance.onError (async + platform-channel errors) — installed before runApp, onError returning true, no runZonedGuarded, and ProviderException unwrapped before logging. Do not restate that bootstrap() here.
What this skill owns is the taxonomy of what reaches those handlers: only genuine bugs and unrecoverable states. Every recoverable failure is a typed Result/Failure value that never throws across a layer, so it never reaches the net. Isolate errors are the one thrown category the net misses — re-wrap them as Result at the call site (below).
Isolate error re-wrapping
Anti-patterns
- `catch (_) {}` / bare `catch (e)` — discards type and stack; fatal when the lost data can't be re-fetched. Catch narrowly and log
(e, st). - A user-facing `String` inside a `Failure`/`Exception` — breaks translation, RTL mirroring, numeral rendering. Codes + typed params only.
- `default:` / `case _:` on a sealed switch — a new failure subtype slips through unhandled; you lose compile-time exhaustiveness.
- `throw e;` in a catch — resets the stack to the rethrow line. Use
rethrow. - A low-level exception in the UI —
PlatformException/SqliteException/FormatExceptionreaching a Notifier or widget. Convert it at the adapter. - `throw` inside pure logic for uncertainty — keep it total; return an explicit "cannot within budget" value.
- Catching `Error` subtypes "to be safe" — you are hiding a bug; let it crash in debug.
- A generic `Result<T>` whose error arm is `Exception`/`Object` — no type to switch on; every case gets silently dropped. Type the error as
F extends Failure. - `assert` on a platform/plugin return value — the assert is stripped in release, so the real-device failure becomes total silence. Return a typed failure instead.
- Treating notification/permission/plugin errors as sync — they are async MethodChannel errors; catch at the call site AND rely on
PlatformDispatcher.onError. - A multi-table write without a transaction — a half-applied write silently corrupts derived data with no way to notice offline.
- A second analytics/chart query that bypasses the shared soft-delete filter — deleted rows silently pollute reports. One filter, every read.
Definition of done
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/error-handling-typed-results/SKILL.md
Popularity
- Repository stars
- 22
- Repository forks
- 4
- 7-day trend score
- 16
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/zakariaf/flutter-skills/error-handling-typed-results)