Feature anyhow removal#459
Conversation
WalkthroughThis pull request refactors error handling across the codebase by removing the dependency on the Changes
Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/bitcoin_price.rs (1)
43-51: Enhanced return type for get_price method.The method has been improved to return
Result<f64, MostroError>instead ofOption<f64>, which provides better error information when price retrieval fails. The error handling is properly implemented using map_err and ok_or.Consider adding a more specific error message when a currency is not found, such as including the currency code in the error:
- .ok_or(MostroInternalErr(ServiceError::NoAPIResponse)) + .ok_or(MostroInternalErr(ServiceError::NoAPIResponse(format!("Price for currency '{}' not found", currency))))This would require updating the ServiceError enum to accept a String for the NoAPIResponse variant.
src/db.rs (2)
590-591: Remove debug print statements.These println statements appear to be leftover debugging code that should be removed or replaced with proper logging.
- println!("solver_pubkey: {}", solver_pubkey); - println!("order_id: {}", order_id); + // Use tracing module for logging if needed + tracing::debug!("Checking if solver {} is assigned to order {}", solver_pubkey, order_id);
22-624: Consider extracting the common error mapping pattern into a helper function.Throughout the file, there's a repetitive pattern of mapping SQLx errors to
MostroInternalErr. Creating a helper function would reduce duplication and make the code more maintainable.You could create a helper function like this:
fn map_db_err<T>(result: Result<T, sqlx::Error>) -> Result<T, MostroError> { result.map_err(|e| MostroInternalErr(ServiceError::DbAccessError(e.to_string()))) }Then use it in your code:
- let mut conn = pool - .acquire() - .await - .map_err(|e| MostroInternalErr(ServiceError::DbAccessError(e.to_string())))?; + let mut conn = map_db_err(pool.acquire().await)?;And for query execution:
- .await - .map_err(|e| MostroInternalErr(ServiceError::DbAccessError(e.to_string())))?; + .await + .pipe(map_db_err)?;The
.pipe()method would require thetapcrate or similar function composition utility, but this approach would significantly reduce code duplication.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (16)
Cargo.toml(1 hunks)src/app.rs(1 hunks)src/app/add_invoice.rs(0 hunks)src/app/admin_add_solver.rs(0 hunks)src/app/rate_user.rs(0 hunks)src/app/take_buy.rs(0 hunks)src/app/take_sell.rs(0 hunks)src/bitcoin_price.rs(2 hunks)src/cli.rs(2 hunks)src/cli/settings.rs(8 hunks)src/db.rs(34 hunks)src/lightning/mod.rs(0 hunks)src/main.rs(1 hunks)src/messages.rs(1 hunks)src/scheduler.rs(2 hunks)src/util.rs(1 hunks)
💤 Files with no reviewable changes (6)
- src/app/take_sell.rs
- src/app/take_buy.rs
- src/app/add_invoice.rs
- src/app/rate_user.rs
- src/lightning/mod.rs
- src/app/admin_add_solver.rs
✅ Files skipped from review due to trivial changes (1)
- src/main.rs
🔇 Additional comments (21)
Cargo.toml (1)
53-54: LGTM! The patch configuration is a good approach for local development.The PR successfully removes the
anyhowdependency and adds a local path reference tomostro-corein the patch section. This setup is beneficial for development as it allows you to work with a local version of the core library while implementing the error handling changes.src/app.rs (1)
356-356: Fixed error handling to dereference theMostroErrorcorrectly.The code properly dereferences the error with
*errwhen passing it to themanage_errorsfunction. This change is necessary after switching fromanyhow::Resultto the customMostroErrortype.src/cli.rs (3)
5-5: LGTM! Appropriate import for the new error handling approach.This import properly replaces the previous
anyhow::Resultdependency with the project-specific error type.
32-32: Function signature updated to use the custom error type.The return type now explicitly specifies
MostroErroras the error type, which enhances clarity and type safety.
36-36: Function return values properly wrapped withOk.The return values are now properly wrapped with
Ok()to match the new function signature. This ensures type consistency throughout the error handling chain.Also applies to: 38-38
src/messages.rs (2)
1-1: LGTM! Appropriate import for the new error handling approach.This import properly replaces the previous
anyhow::Resultdependency with the project-specific error type.
7-7: Function signature updated to use the custom error type.The return type now explicitly specifies
MostroErroras the error type, which enhances clarity and type safety.src/scheduler.rs (4)
12-13: Appropriate import of custom error types.Good addition of imports for the custom MostroError and ServiceError types that are part of the PR's main goal to replace anyhow.
235-235: Updated function signature correctly.The function signature has been properly updated to return
Result<(), MostroError>instead ofanyhow::Result<()>, which aligns with the PR objective of removing anyhow dependency.
240-244: Improved error handling with specific error types.Error handling has been properly refactored to use the custom error type hierarchy. This provides more specific error information compared to the previous anyhow implementation.
248-252: Consistent error handling pattern.The error handling for key retrieval follows the same pattern as the database connection error handling, maintaining consistency throughout the codebase.
src/util.rs (1)
542-542: Updated return type for invoice_subscribe function.The function signature has been properly changed from
anyhow::Result<()>toResult<(), MostroError>to align with the PR objective of removing anyhow dependency.src/bitcoin_price.rs (3)
1-2: Proper imports for custom error types.Good addition of the necessary imports for MostroError and ServiceError to implement the anyhow removal strategy.
23-31: Enhanced error handling in update_prices method.The method's return type has been correctly changed to
Result<(), MostroError>and HTTP request error handling has been improved with specific error types, making debugging and error handling more effective.
36-39: Consistent error handling for RwLock operations.Error handling for RwLock write operations follows the same pattern as other error handling in the file, converting lock errors to ServiceError::IOError.
src/cli/settings.rs (5)
3-4: Appropriate imports for custom error types.Good addition of the necessary imports to implement the anyhow removal strategy across the codebase.
50-54: Updated error type for TryFrom implementation.The error type for the Database TryFrom implementation has been correctly updated to use MostroError, aligning with the PR objective.
70-74: Consistent error type updates across all TryFrom implementations.All TryFrom implementations for the different configuration structs (Lightning, Nostr, Mostro) have been updated consistently to use MostroError as the error type.
Also applies to: 84-88, 107-111
173-173: Updated function signature for init_default_dir.The function signature has been properly updated to return
Result<PathBuf, MostroError>instead of using anyhow's Result type.
187-189: Enhanced error handling with specific error types.The error handling in init_default_dir has been improved to use map_err with specific ServiceError variants (EnvVarError, IOError), providing better context for troubleshooting when errors occur.
Also applies to: 204-213
src/db.rs (1)
22-73: LGTM! Good error handling refactoring.The removal of anyhow and introduction of the more specific
MostroErrortype improves the API of this module by making the error types explicit in the function signatures.
* 📝 Add docstrings to `feature-full-privacy-mode-checks` (#455) * add feature to check correctly if order is full privacy or normal * 📝 Add docstrings to `feature-full-privacy-mode-checks` Docstrings generation was requested by @arkanoider. * #454 (comment) The following files were modified: * `src/app/order.rs` * `src/util.rs` --------- Co-authored-by: arkanoider <github.913zc@simplelogin.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: arkanoider <113362043+arkanoider@users.noreply.github.com> * Fix network in order event (#452) Now in orers event 38383 the network that comes out is the correct one * Feature anyhow removal (#459) * remove of anyhow dependency start * removed anyhow dependecy * remove patch from cargo.toml * Update mostro-core version --------- Co-authored-by: Francisco Calderón <fjcalderon@gmail.com> * Feature disputes (#463) * Feature: dispute fix for new logic * fix for coop cancel case * Add logic for info message about dispute for solver * first working dispute info message for admin after taking disputes * added fields to dispute info message for solver * removed some probably useless and refactored a bit admin-take-dispute function * clippy suggestion * other cosmetics to admin take file * some other cosmetics on admin-take-dispute file * fix on admin-take * Update admin_take_dispute.rs * fixed a wrong search of order id in admin take file@ * Fix: added correct check to add solver from admin cli * fix: wrong check on admin cancel * Fix initiator pubkey: now is the trade key of initiator not identity * quick fix for full privacy orders * new logic with mixed full privacy - regular reputation mode * Update cargo toml to compile with version 0.6.32 of mostro-core - this is in preparation for migrating to nostr-sdk 0.40 * rollback sdk to 0.38 version to merge disputes - we will come back to 0.40 when we will fix signature issue * Update mostro-core dependency --------- Co-authored-by: Francisco Calderón <fjcalderon@gmail.com> * added correction for the case of buyer adding back a new invoice after payment failure (#462) * Fix for nostr sdk 40 issue on incoming message (#465) * testing sdk 40 * fix for sdk 40 * cleaned cargo.toml * Bump mostro core version --------- Co-authored-by: Francisco Calderón <fjcalderon@gmail.com> * fix: sends order with an updated 'status' field as the reply to add-invoice when there is a preimage (#464) * Feature-nip69-order-status (#467) * feature: align to nip69 order status in nostr event * Improved in progress nip69 logic * refined event states of the order with nip69 request * add another check to avoid multiple events with in-progress state * Privacy range order fix (#468) * add optional field for users in full privacy inside solver message * To be tested - privacy range child order fix * fix cargo.toml * bumped mostro-core version to 6.35 * New user or privacy order send same user info with zeros (#471) * new user or privacy order send same user info with zeros * fix days field and removed some println! macro with nicer tracing messages * bumped mostro-core version to 6.36 and added requested fix to compile * Add previous order state in database for solver message (#472) * add previous state in database for solver message * bumped mostro-core version to 6.36 * Taker info message to maker. I think that could be modifies in a more generic UserInfo. (#473) * added logic to send infos to maker when an order is taken * Add takers info message for maker * fix cargo.toml * set peer pubkey to empty string when notifying taker reputation to maker * moved user info message to the right point of the order flow * fix cargo.toml * changed message struct from UserDisputeInfo to UserInfo * fix: get master keys for user infos * Bumped mostro-core to version 6.38 * fix for buy order flow with message to maker (#479) * Rabbit fixes --------- Co-authored-by: Francisco Calderón <fjcalderon@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Catrya <140891948+Catrya@users.noreply.github.com> Co-authored-by: Bilthon <bilthon@gmail.com>
@grunch
removed anyhow dependency.
Summary by CodeRabbit
Chores
anyhowdependency to streamline the project.Refactor
MostroErrorfor enhanced error specificity.