Skip to content

Conversation

@lowhung
Copy link
Collaborator

@lowhung lowhung commented Nov 5, 2025

Description

This PR refactors the REST handler error handling to use RESTError consistently instead of anyhow. This takes the work from QueryError in the state modules and creates what I think is a better error handling flow.

Changes:

  • Updated all handler function signatures from Result<RESTResponse> to Result<RESTResponse, RESTError>
  • Updated query_state closures to return QueryError instead of wrapping in anyhow
  • Added proper error conversion from QueryError to RESTError via the From trait
  • Standardized parameter validation using RESTError::invalid_param(), RESTError::param_missing(), etc.

Related Issue(s)

Part of #313

How was this tested?

  • Code builds successfully
  • Existing unit tests pass
  • Manual testing of some endpoints (if applicable)

Checklist

  • My code builds and passes local tests
  • CI is green for this PR

Impact / Side effects

Error responses maintain the same HTTP status codes and structure.

…or consistent error handling across modules, replacing raw strings or `anyhow::Error`
@lowhung lowhung marked this pull request as draft November 5, 2025 19:30
@lowhung lowhung force-pushed the lowhung/313-query-error-type branch from c103432 to 6aa66de Compare November 5, 2025 23:58
@lowhung lowhung force-pushed the lowhung/313-query-error-type branch from 3ba464a to 1bc9e65 Compare November 6, 2025 00:15
@lowhung lowhung changed the base branch from main to lowhung/introduce-new-error-types November 6, 2025 00:33
Base automatically changed from lowhung/introduce-new-error-types to main November 6, 2025 17:30
# Conflicts:
#	modules/accounts_state/src/accounts_state.rs
#	modules/address_state/src/address_state.rs
#	modules/assets_state/src/assets_state.rs
#	modules/chain_store/src/chain_store.rs
#	modules/drep_state/src/drep_state.rs
#	modules/epochs_state/src/epochs_state.rs
#	modules/governance_state/src/governance_state.rs
#	modules/historical_accounts_state/src/historical_accounts_state.rs
#	modules/rest_blockfrost/src/handlers/accounts.rs
#	modules/rest_blockfrost/src/handlers/addresses.rs
#	modules/rest_blockfrost/src/handlers/assets.rs
#	modules/rest_blockfrost/src/handlers/blocks.rs
#	modules/rest_blockfrost/src/handlers/epochs.rs
#	modules/rest_blockfrost/src/handlers/governance.rs
#	modules/rest_blockfrost/src/handlers/pools.rs
#	modules/spo_state/src/spo_state.rs
#	modules/utxo_state/src/utxo_state.rs
@lowhung lowhung marked this pull request as ready for review November 7, 2025 19:37
@lowhung lowhung force-pushed the lowhung/313-query-error-type branch from 8cb0a23 to 189edfb Compare November 7, 2025 19:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR standardizes error handling across REST endpoints by introducing RESTError and updating QueryError types. The changes eliminate anyhow::Result in favor of strongly-typed error handling, providing more consistent HTTP status codes and error messages.

Key changes:

  • Handler signatures updated from Result<RESTResponse> to Result<RESTResponse, RESTError>
  • Query state closures now return QueryError instead of wrapping errors in anyhow
  • Standardized parameter validation using RESTError::invalid_param(), RESTError::param_missing(), etc.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
modules/spdd_state/src/rest.rs Updated SPDD handler to use RESTError
modules/rest_blockfrost/src/rest_blockfrost.rs Updated handler registration signatures and cleaned up imports
modules/rest_blockfrost/src/handlers/pools.rs Refactored pool handlers to use RESTError with proper error conversion
modules/rest_blockfrost/src/handlers/governance.rs Updated governance handlers to use RESTError and standardized message bus error handling
modules/rest_blockfrost/src/handlers/epochs.rs Updated epoch handlers with RESTError and improved error messages
modules/rest_blockfrost/src/handlers/blocks.rs Simplified block handlers by removing QueryError::NotFound handling
modules/rest_blockfrost/src/handlers/assets.rs Refactored asset handlers to use RESTError consistently
modules/rest_blockfrost/src/handlers/addresses.rs Updated address handlers with RESTError and improved error context
modules/rest_blockfrost/src/handlers/accounts.rs Refactored account handlers with RESTError and improved error handling
modules/drdd_state/src/rest.rs Updated DRDD handler to use RESTError with storage_disabled check
common/src/rest_helper.rs Updated REST helper functions to handle RESTError properly
common/src/rest_error.rs Fixed param_missing message format
common/src/queries/utils.rs Updated query_state and rest_query_state to use QueryError
common/src/queries/errors.rs Added Fromanyhow::Error implementation for QueryError

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# Conflicts:
#	modules/rest_blockfrost/src/handlers/accounts.rs
@lowhung lowhung requested a review from Copilot November 7, 2025 23:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

modules/rest_blockfrost/src/handlers/accounts.rs:534

  • This error handling wasn't updated to use the new error pattern. It should be:
let json = serde_json::to_string_pretty(&rest_response)?;
Ok(RESTResponse::with_json(200, &json))

This is inconsistent with the rest of the refactoring where serde_json errors are automatically converted to RESTError via the From trait.

    match serde_json::to_string_pretty(&rest_response) {
        Ok(json) => Ok(RESTResponse::with_json(200, &json)),
        Err(e) => Ok(RESTResponse::with_text(
            500,
            &format!("Internal server error while serializing addresses: {e}"),
        )),
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Err(resp) => return Ok(resp),
};
) -> Result<RESTResponse, RESTError> {
let account = parse_stake_address(&params)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

&format!("Internal server error while retrieving account info: {e}"),
)),
}
let json = serde_json::to_string_pretty(&rest_response)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also really nice!

Copy link
Collaborator

@whankinsiv whankinsiv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice job Matt! I really like how this PR cleans up the parameter parsing and serialization error handling.

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.

4 participants