Skip to content

refactor(utils): centralize error handling in shared utils crate#62

Merged
Soushi888 merged 2 commits into
devfrom
refactor/60-centralize-errors
Mar 18, 2026
Merged

refactor(utils): centralize error handling in shared utils crate#62
Soushi888 merged 2 commits into
devfrom
refactor/60-centralize-errors

Conversation

@Soushi888

@Soushi888 Soushi888 commented Mar 18, 2026

Copy link
Copy Markdown
Collaborator

Intent

All three coordinator zomes defined their own error enums inline in lib.rs, duplicating
the same infrastructure variants (SerializationError, EntryOperationFailed,
LinkOperationFailed, InvalidInput) across three files. Five .expect() calls in the
resource and person zomes would crash the WASM instead of returning proper errors. The
thiserror version was pinned at 1.0 in three places while the workspace declared 2.0.

This unifies all error types in crates/utils/src/errors.rs before the hREA bridge (Epic
#25) multiplies these patterns further.

Changes

Centralized error types: PersonError, ResourceError, and GovernanceError are
removed from each coordinator lib.rs and defined once in crates/utils/src/errors.rs.
Each zome re-exports its error type via pub use nondominium_utils::errors::XxxError so
all existing call sites (use crate::XxxError) compile without modification.

Expanded CommonError: The shared CommonError in utils gains infrastructure variants
(EntryNotFound, RecordNotFound, LinkError, ActionHashNotFound) to cover DHT-level
error patterns currently handled ad-hoc per zome.

Panic elimination: Five .expect() calls converted to proper error handling:

  • 4 link-target conversions in zome_resource changed from .map().expect() to
    .filter_map() — silently drops unconvertible link targets instead of panicking
  • 1 Option unwrap in zome_person capability sharing changed to .ok_or(PersonError::...)?

Version consistency: thiserror in crates/utils, zome_person, and zome_resource
Cargo.toml changed from pinned "1.0" to { workspace = true } (v2.0), matching the
workspace declaration and zome_gouvernance which was already correct.

Decisions

Option Rejected because
Move infrastructure variants to CommonError, keep only domain variants in domain enums Increases migration scope and breaks call sites that use XxxError::SerializationError(...) — deferred to a follow-up once call sites stabilize
Use .map() + collect::<Result<Vec<_>>>()? for link-target fixes Returns an error on any single bad link, which is too strict in a DHT context where partial data is expected; filter_map is the appropriate "skip and continue" pattern

How to test

# 1. Verify no .expect() panics remain
grep -r "\.expect(" dnas/ --include="*.rs"

# 2. Build succeeds
nix develop --command bun run build:zomes

# 3. Full test suite
nix develop --command bun run tests

CI runs bun run build:zomes on every push.

Documentation

Updated documentation/zomes/person_zome.md, resource_zome.md, and governance_zome.md
to note that each error type is now defined in crates/utils/src/errors.rs.

Related

- Create crates/utils/src/errors.rs with CommonError (expanded),
  PersonError, ResourceError, GovernanceError — each with From<E> for WasmError
- Remove inline error enum definitions from all three coordinator zomes
- Re-export error types from nondominium_utils::errors in each zome lib.rs
- Fix thiserror version in crates/utils, zome_person, zome_resource to
  use workspace = true (v2.0), matching workspace declaration
- Convert 5 .expect() panics to proper error handling:
  - 4 link-target conversions in zome_resource changed from .map().expect()
    to .filter_map() pattern
  - 1 Option unwrap in capability_based_sharing changed to .ok_or()?

Closes #60
@Soushi888 Soushi888 marked this pull request as ready for review March 18, 2026 00:17
@Soushi888 Soushi888 changed the base branch from main to dev March 18, 2026 00:25
@Soushi888 Soushi888 merged commit 4c189f4 into dev Mar 18, 2026
1 check passed
@Soushi888 Soushi888 deleted the refactor/60-centralize-errors branch March 18, 2026 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(utils): centralize error handling in shared utils crate

1 participant