Skip to content
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

[ENH]: GET /databases should return 404 instead of 500 for not found #2788

Merged
merged 3 commits into from
Sep 12, 2024

Conversation

codetheweb
Copy link
Contributor

Description of changes

When a database is not found, a 404 HTTP status code is now returned instead of a 500.

Test plan

How are these changes tested?

Added a new test. It doesn't strictly test the status code (difficult if we want to keep the more friendly error message), but exercises the code path.

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs repository?

n/a

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@codetheweb codetheweb force-pushed the fix-database-not-found-500 branch from 93f5b9b to d30bb50 Compare September 12, 2024 21:46
@codetheweb codetheweb marked this pull request as ready for review September 12, 2024 22:18
@codetheweb codetheweb requested a review from atroyn September 12, 2024 22:19
@atroyn atroyn self-assigned this Sep 12, 2024
Copy link
Contributor

@atroyn atroyn left a comment

Choose a reason for hiding this comment

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

I'm a bit confused by this implementation, the new error is already a ChromaError so it should be already getting caught by:

except ChromaError as e:
            raise e

Why do we need a separate exception path like

except Exception as e:
            if isinstance(e, NotFoundError):
            ...

f"Could not connect to database {database} for tenant {tenant}. Are you sure it exists?"
)
except Exception as e:
if isinstance(e, NotFoundError):
Copy link
Contributor

Choose a reason for hiding this comment

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

why not except NotFoundError ?

@codetheweb
Copy link
Contributor Author

I'm a bit confused by this implementation, the new error is already a ChromaError so it should be already getting caught by:

it's just

 except Exception as e:
    if isinstance(e, NotFoundError):
        raise ValueError(
            f"Could not connect to database {database} for tenant {tenant}. Are you sure it exists?"
        )

    raise e

vs.

except NotFoundError:
  raise ValueError(
        f"Could not connect to database {database} for tenant {tenant}. Are you sure it exists?"
    )
except Exception as e:
  raise e

Functionally the same but the second one does look cleaner, so I'll change.

@atroyn
Copy link
Contributor

atroyn commented Sep 12, 2024

Discussed offline, ChromaError allows us to set the message directly at the callsite by overriding message:

class ChromaError(Exception, EnforceOverrides):
    trace_id: Optional[str] = None
    def code(self) -> int:
        """Return an appropriate HTTP response code for this error"""
        return 400  # Bad Request
    def message(self) -> str:
        return ", ".join(self.args)

So let's just use that

Copy link
Contributor

@atroyn atroyn left a comment

Choose a reason for hiding this comment

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

LGTM

@codetheweb codetheweb enabled auto-merge (squash) September 12, 2024 23:16
@codetheweb codetheweb merged commit 64ecc8e into main Sep 12, 2024
68 checks passed
@codetheweb codetheweb deleted the fix-database-not-found-500 branch September 13, 2024 18:18
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.

2 participants