Error and result handling
This document describes how TrustWeave surfaces failures across layers. Follow it for new APIs and refactors.
Principles
-
Expected domain failures — Use a sealed result type (
DidCreationResult,IssuanceResult,DidResolutionResult, KMS*Result, etc.) at public boundaries so callers can handle failures exhaustively without catching exceptions. -
Programming errors — Use
require,check, orIllegalArgumentException/IllegalStateExceptiononly for bugs or impossible states the caller is responsible for (e.g. missing mandatory DSL field beforebuild()). Document when the DSL throws. -
Invalid user or configuration input — Prefer structured failures on the result type (e.g.
DidCreationResult.Failure.InvalidConfiguration) orTrustWeaveException.ValidationFailedwhen an exception-based API is required. -
Infrastructure / I/O faults — Use
TrustWeaveExceptionor a domain-specific subclass (e.g.BlockchainException) with a stablecodeand structuredcontext. Preservecausewhen wrapping third-party errors. -
KMS operations — Implementations return sealed
org.trustweave.kms.results.*types. UsegetOrThrow()only at boundaries that intentionally translate to exceptions.
DID creation
createDidreturnsDidCreationResult.createDidWithKeyreturnsDidCreationWithKeyResult: success includes(Did, keyId); failures either wrapDidCreationResult.Failureor describe key extraction errors.
TrustWeave facade vs services
- TrustWeave composes domain services; DID operations (
createDid,resolveDid, etc.) are members on the facade. DID DSL builders andDidManagementServicedepend onDidDslContext, not the full facade type.
Credential API layout
- SPI for format conversion:
org.trustweave.credential.spi.transform(CredentialFormatConverter). - Default Jackson/Nimbus/CBOR implementation:
org.trustweave.credential.transform(CredentialTransformerand extension helpers). Callers normally useCredentialServiceextensions inCredentialServiceExtensions.ktrather than constructing a transformer directly.
Backlog / hotspots
When adding features, prefer aligning with the above over introducing new ad-hoc exception types for expected failures. Document any remaining kotlin.Result or exception-heavy surfaces in PR descriptions until migrated.