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

Extend the documentation with respect to the exceptions being raised by various functions #531

Closed
ghost opened this issue May 27, 2015 · 10 comments

Comments

@ghost
Copy link

ghost commented May 27, 2015

The function discover_repository apparently raises KeyError if it cannot find a repository. I think that it is not appropriate and it also is not documented. Could it be fixed, please?

@carlosmn
Copy link
Member

Fixed how? Without knowing what you expect it to do, it's not possible to provide any kind of fix.

KeyError is an odd class, and it's a side-effect of python having different ideas for what libgit2 returns as a GIT_ENOTFOUND error. There's a boolean to pass for IO contexts, which we apparently forgot; but what do you expect it to do if not found? Raise a different exception? Do something else?

@ghost
Copy link
Author

ghost commented May 28, 2015

Basically it's up to you. If you are OK with the KeyError, I can live with that. But what I do care about is the documentation. Could you please document what happens if no repository is found?

@jdavid
Copy link
Member

jdavid commented May 28, 2015

Python usually raises IOError with errno 2 (ENOENT) when it does not find something in the filesystem. But in other cases it raises OSError (with the same errno).

@ghost
Copy link
Author

ghost commented May 28, 2015

I believe that IOError and OSError are raised if the function actually tries to access a directory/file and this attempt fails. I don't know how the function works but if it doesn't try to open/read/write/remove/list a directory/file, I think that a ValueError or LookupError would be fine. Or it can return None. It depends on the implementation. It may be a combination of all of them.

@ghost ghost changed the title Document the exception raised by discover_repository Extend the documentation with respect to the exceptions being raised by various functions Jun 2, 2015
@ghost
Copy link
Author

ghost commented Jun 2, 2015

Let me make the bug report broader.
Various functions/methods raise various exceptions. Sometimes, they are reasonable but just in case that you know the implementation of the functions. E.g. pygit2.init_repository raises ValueError in case of a wrong path. On the other hand, pygit2.Repository.index.add raises IOError. It would be nice to make the exceptions raised by the functions consistent, or document the implementation of the functions or document what exceptions raise which function. Some examples (not the complete list):

  • pygit2.init_repository in case of an inaccessible path
  • pygit2.Index.add in case of an inaccessible path
  • pygit2.Index.write in case of wrong permissions
  • pygit2.Repository.reset in case of wrong oid

Maybe it would be worth mentioning what happens if another process removes the repository files in the meantime as well.

@pypingou
Copy link

pypingou commented Jun 2, 2015

One possibility would be to also use custom-class errors allowing consumer to catch the high-level exception (from which all pygit2 exceptions inherit) and display the corresponding error message

@kbd
Copy link

kbd commented May 19, 2018

I just started experimenting with pygit2 and pretty much the first thing you run into (just running the example code) is that discover_repository throws a KeyError if you're not in a repository.

This is unexpected for three reasons:

  1. it's not documented. I went through and read the source to make sure this was expected behavior and I wasn't doing something wrong.
  2. in Python you don't expect KeyError to be raised from function calls. KeyError is used for accessing a non-existent key in a dictionary.
  3. The most natural thing for discover_repository to do is return None to indicate no repository found.

Throwing an exception at all for a normal case of looking-for-and-not-discovering a repository was surprising, but a KeyError was doubly surprising because there's no mapping type involved, so it gives the impression that it's an internal error that escaped. If it threw a RepositoryNotFoundError (or a GitError with an exception message) then it would be clearer that that was expected behavior.

Of course discover_repository can't be changed now without breaking clients, but documentation of the exception would be helpful.

@jdavid
Copy link
Member

jdavid commented May 20, 2018

Now it returns None and it's documented.

This issue became broader than just discover_repository, any suggestions for what should raise/return the other functions?

@kbd
Copy link

kbd commented May 20, 2018

Now it returns None and it's documented.

👍

any suggestions for what should raise/return the other functions?

I think it's fine if a function that creates files throws an IOError if (eg.) the filesystem is un-writeable, or a ValueError when passed something of the right type but wrong value. The problem with discover_repository's KeyError is that a key error is not what happened. Documenting the common exceptions that your function can throw is helpful.


The other approach is to catch and wrap everything that comes out of your library in a library-specific exception, and not just GitError (because then you lose information, or have to inspect the inner exception) but a specific subclass of GitError for the particular way something went wrong. I think that's a lot of work for not much benefit, and if you decide to go the other way later you can always make your exception inherit from the thing it's replacing so that FileSystemUnwriteableError(GitError, IOError) is still catchable with except IOError.

netbsd-srcmastr referenced this issue in NetBSD/pkgsrc Jun 5, 2018
0.27.1 (2018-06-02)
-------------------------

Breaking changes:

- Now ``discover_repository`` returns ``None`` if repository not found, instead
  of raising ``KeyError``
  `#531 <https://github.com/libgit2/pygit2/issues/531>`_

Other changes:

- New ``DiffLine.raw_content``
  `#610 <https://github.com/libgit2/pygit2/issues/610>`_

- Fix tests failing in some cases
  `#795 <https://github.com/libgit2/pygit2/issues/795>`_

- Automatize wheels upload to pypi
  `#563 <https://github.com/libgit2/pygit2/issues/563>`_
kbd added a commit to kbd/setup that referenced this issue Jun 7, 2018
@jdavid
Copy link
Member

jdavid commented Oct 20, 2018

Closing, follow up in issue #830

@jdavid jdavid closed this as completed Oct 20, 2018
netbsd-srcmastr referenced this issue in NetBSD/pkgsrc Dec 1, 2018
0.27.2 (2018-09-16)
-------------------------

- Add support for Python 3.7
  `#809 <https://github.com/libgit2/pygit2/issues/809>`_

- New ``Object.short_id``
  `#799 <https://github.com/libgit2/pygit2/issues/799>`_
  `#806 <https://github.com/libgit2/pygit2/pull/806>`_
  `#807 <https://github.com/libgit2/pygit2/pull/807>`_

- New ``Repository.descendant_of`` and ``Repository.branches.with_commit``
  `#815 <https://github.com/libgit2/pygit2/issues/815>`_
  `#816 <https://github.com/libgit2/pygit2/pull/816>`_

- Fix repository initialization in ``clone_repository(...)``
  `#818 <https://github.com/libgit2/pygit2/issues/818>`_

- Fix several warnings and errors, commits
  `cd896ddc <https://github.com/libgit2/pygit2/commit/cd896ddc>`_
  and
  `dfa536a3 <https://github.com/libgit2/pygit2/commit/dfa536a3>`_

- Documentation fixes and improvements
  `#805 <https://github.com/libgit2/pygit2/pull/805>`_
  `#808 <https://github.com/libgit2/pygit2/pull/808>`_


0.27.1 (2018-06-02)
-------------------------

Breaking changes:

- Now ``discover_repository`` returns ``None`` if repository not found, instead
  of raising ``KeyError``
  `#531 <https://github.com/libgit2/pygit2/issues/531>`_

Other changes:

- New ``DiffLine.raw_content``
  `#610 <https://github.com/libgit2/pygit2/issues/610>`_

- Fix tests failing in some cases
  `#795 <https://github.com/libgit2/pygit2/issues/795>`_

- Automatize wheels upload to pypi
  `#563 <https://github.com/libgit2/pygit2/issues/563>`_
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

No branches or pull requests

4 participants