Replace anyhow with custom error types using thiserror#1192
Conversation
Replace anyhow::Result with a structured Error enum using thiserror. This gives consumers typed errors they can match on instead of opaque anyhow errors. - Add Error enum with #[from] for all external error types (serde_json, base64, io, jsonwebtoken, elliptic-curve, rsa, aws-lc-rs, reqwest) - Add KeyError enum for key-specific errors (invalid algorithm/curve, missing private key, unsupported operations, key not found, etc.) - Export crate::Result<T> type alias - Update all public API methods to return crate::Result<T> - All 88 existing tests pass unchanged https://claude.ai/code/session_01AaDSYmsVmbhVA6iSFJ6yNa
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (5)
WalkthroughThe pull request replaces 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@rs/moq-token/src/generate.rs`:
- Around line 79-81: The code accesses RSA CRT fields via key.dp(), key.dq(),
and key.qinv() and uses expect(), which will panic because those Options are
only populated after precompute(); call key.precompute().map_err(|e| …)? (or
otherwise invoke the rsa crate's precompute method) before reading dp/dq/qinv in
generate.rs, then replace the expect() calls with proper error propagation
(e.g., .ok_or_else(|| YourError::MissingCrtField("dp"))? ) so the function
returns a Result on failure instead of panicking.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 24048dc1-112f-4110-89e8-06482215109c
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (8)
rs/moq-token/Cargo.tomlrs/moq-token/src/algorithm.rsrs/moq-token/src/claims.rsrs/moq-token/src/error.rsrs/moq-token/src/generate.rsrs/moq-token/src/key.rsrs/moq-token/src/lib.rsrs/moq-token/src/set.rs
…error-SN5wz # Conflicts: # rs/moq-token/src/key.rs
- Resolve merge conflict in key.rs: keep simplified error handling from PR while adding guest/guest_sub/guest_pub fields from main - Address CodeRabbit review: replace expect() with proper error handling for RSA CRT fields (dp/dq/qinv) by calling precompute() first - Apply cargo-sort and formatter fixes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This PR replaces the
anyhowcrate with a custom error type hierarchy usingthiserror, providing more structured and type-safe error handling throughout the moq-token crate.Summary
Migrated from generic
anyhow::Errorto a customErrorenum with a specializedKeyErrorvariant for cryptographic operations. This improves error semantics, enables better error handling patterns, and reduces external dependencies.Key Changes
New error module (
error.rs): IntroducedKeyErrorenum for key-specific errors and top-levelErrorenum that encompasses all error types in the crateKeyErrorvariants cover algorithm mismatches, missing keys, unsupported operations, and coordinate validationErrorenum aggregatesKeyErrorand integrates with external error types (JSON, IO, Base64, JWT, RSA, etc.)Result<T>type alias asstd::result::Result<T, Error>Updated
key.rs: Replaced allanyhow::Resultwithcrate::Resultand converted error handling:bail!()andanyhow::anyhow!()with explicitKeyErrorvariants?operator instead of match expressionsContexttrait usage, usingok_or()for Option conversionsUpdated
generate.rs: Migrated to custom error types and simplified RSA key generation error handlingUpdated
set.rs: Replacedanyhow::Resultwithcrate::Resultand removed verbose error wrapping into_public_set()Updated
algorithm.rs: ChangedFromStr::Errtype fromanyhow::ErrortoErrorwith proper variant usageUpdated
claims.rs: Replacedanyhow::bail!()withcrate::Error::UselessTokenUpdated
Cargo.toml: Replacedanyhow = "1"dependency withthiserror = "2"Notable Implementation Details
#[from]attributes for automatic conversion from underlying error typesResult<T>)jwks-loaderfeature errorshttps://claude.ai/code/session_01AaDSYmsVmbhVA6iSFJ6yNa