Skip to content

Categorize crates.io API errors #2852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

byfnoel
Copy link

@byfnoel byfnoel commented Jun 29, 2025

This PR improves error handling for queries to the crates.io API by categorizing error responses and updating how they are surfaced to users. Instead of logging 4xx or 5xx errors and reporting them to sentry, now we simply show them directly to the user.

Previously, all errors from the crates.io API were logged and reported to Sentry. By surfacing these errors to users, we get to reduce noise in the tracking system and provide more descriptive responses.

Closes #2480

@github-actions github-actions bot added the S-waiting-on-review Status: This pull request has been implemented and needs to be reviewed label Jun 29, 2025
@byfnoel byfnoel marked this pull request as ready for review June 29, 2025 23:04
@byfnoel byfnoel requested a review from a team as a code owner June 29, 2025 23:04
@GuillaumeGomez
Copy link
Member

Looks good to me, thanks! I'll let @syphar take a look as well.

@byfnoel
Copy link
Author

byfnoel commented Jul 1, 2025

Thank you for the review @GuillaumeGomez. Let's see if @syphar wants anything changed.

Copy link
Member

@syphar syphar left a comment

Choose a reason for hiding this comment

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

Thank you for the good work! I'm very happy to see this coming.

//Error message to gracefully display
fn create_search_error_response(query: String, sort_by: String, error_message: String) -> Search {
Search {
title: format!("Search service is not currently available: {error_message}"),
Copy link
Member

Choose a reason for hiding this comment

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

please add a test that tests this new behaviour.

@@ -233,6 +253,35 @@ async fn get_search_results(
})
}

// Categorize errors from registry
fn handle_registry_error(err: anyhow::Error) -> Result<SearchResult, SearchError> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn handle_registry_error(err: anyhow::Error) -> Result<SearchResult, SearchError> {
fn handle_registry_error(err: anyhow::Error) -> SearchError {

the Ok variant will never be returned, right?

Copy link
Member

Choose a reason for hiding this comment

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

also, we could put this into from impl From<anyhow::Error> for SearchError above?

Copy link
Author

Choose a reason for hiding this comment

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

Note: The OK variant will never return.

Also, I don't feel comfortable with returning SearchError directly. While the suggestion makes it simpler, it could lead to issues down the line when the function would fail silently. I'd prefer this to stay explicit.

&& let Some(status) = registry_request_error.status()
&& (status.is_client_error() || status.is_server_error())
{
return Err(SearchError::CratesIo(format!(
Copy link
Member

Choose a reason for hiding this comment

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

looking at RegistryApi::search, I can see other errors using bail! that might also be interesting for the user.

what do you think? should we change RegistryApi::search to return SearchError too?

Copy link
Author

Choose a reason for hiding this comment

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

Great suggestion. However, I have great reserve about this because RegistryApi might introduce new problems such as limited reusability, and how we might want to handle errors in different part of the codebase.

@byfnoel byfnoel force-pushed the categorize-errors branch from c697b6b to 5f81817 Compare July 4, 2025 18:09
Co-authored-by: Denis Cornehl <denis@cornehl.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: This pull request has been implemented and needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

show crates.io search errors to the user without logging them
3 participants