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

Generic exceptions vs Specific exceptions #1784

Closed
MVrachev opened this issue Jan 19, 2022 · 4 comments
Closed

Generic exceptions vs Specific exceptions #1784

MVrachev opened this issue Jan 19, 2022 · 4 comments
Assignees
Labels
1.0.0 blockers To be addressed before 1.0.0 release
Milestone

Comments

@MVrachev
Copy link
Collaborator

MVrachev commented Jan 19, 2022

Description of issue or feature request:
We want to use a shortlist of exceptions that will be thrown from our APIs, so that our users can handle them easily.
On another hand that doesn't stop us from creating other more specific exceptions that derive from those main exceptions.
This allows us to easily test that specific errors were thrown when we expected them.
For example, we are using BadVersionError and LengthOrHashMismatchError which derive from RepositoryError.

Still, there are two use cases that are worth discussing:

  1. There are multiple places inside TrustedMetadataSet where in one API call we are throwing generic and specific exceptions.
    That's happening inside each of the functions update* inside TrustedMetadataSet we throw RepositoryError, ReplayedMetadataError, ExpiredMetadataError and UnsignedMetadataError (from verify_delegate()) at the same time.
    One idea could be to define new specific exceptions (which derive from RepositoryError and replace the instances of RepositoryError with them.

Read and synchronize with issues:

  1. Inside the TrustedMetadataSet update* function docstrings we only document that we are throwing RepositoryError without specifying the specific exceptions. If none of them are documented in the Raises section of the method docstrings (they only mention the broad RepositoryError), do we expect anyone to handle them? If we do, we should probably document them?

Inspired by #1725 (comment).

  1. Inside _get_session in module tuf/ngclient/_internal/request_fetcher.py we currently throw URLParsingError:
    raise exceptions.URLParsingError(

    In my commit e8f26eb inside pr Add new exceptions file for exceptions in the new code #1725 I am replacing it with DownloadError.
    The question is do we want to keep the specific exception - URLParsingError and make it derive from DownloadError?
    Does my change in this commit make sense?
@jku
Copy link
Member

jku commented Jan 20, 2022

The question is do we want to keep the specific exception - URLParsingError and make it derive from DownloadError?
Does my change in this commit make sense?

I believe no-one is ever going to handle a URLParsingError so there should be no need for it. If that statement turns out wrong and a need arises later, adding a derived error is easy. The commit is good.

@jku
Copy link
Member

jku commented Jan 20, 2022

Inside the TrustedMetadataSet update* function docstrings we only document that we are throwing RepositoryError without specifying the specific exceptions. If none of them are documented in the Raises section of the method docstrings (they only mention the broad RepositoryError), do we expect anyone to handle them? If we do, we should probably document them?

Updater does use some of the specific exceptions. I would not object to TrustedMetadataSet having better exception documentation, the reason it doesn't is probably the fact that it's currently private (documenting too much does lead to outdated documentation...)

For updater itself I think documenting just RepositoryError (and not the more specific ones) is reasonable: Updater users are not going to care about the details.

@lukpueh lukpueh self-assigned this Jan 26, 2022
@lukpueh lukpueh added the 1.0.0 blockers To be addressed before 1.0.0 release label Jan 26, 2022
@jku jku added this to the sprint 16 milestone Jan 26, 2022
@lukpueh
Copy link
Member

lukpueh commented Jan 27, 2022

I just did a thorough review of all exceptions we raise in api and ngclient and it all looks good modulo some missing documentation (will post related comments to #1785, where they seem more appropriate).

So I agree with what Jussi says about TrustedMetadataSet exceptions and I suggest we remove the following TODO comment?

* exceptions are not final: the idea is that client could just handle
a generic RepositoryError that covers every issue that server provided
metadata could inflict (other errors would be user errors), but this is not
yet the case

Let's do that and close this issue?

@jku
Copy link
Member

jku commented Jan 28, 2022

Yes: PR #1805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0.0 blockers To be addressed before 1.0.0 release
Projects
None yet
Development

No branches or pull requests

3 participants