Blame docs/error-handling.md

Packit ae9e2a
Error reporting in libgit2
Packit ae9e2a
==========================
Packit ae9e2a
Packit ae9e2a
Libgit2 tries to follow the POSIX style: functions return an `int` value
Packit ae9e2a
with 0 (zero) indicating success and negative values indicating an error.
Packit ae9e2a
There are specific negative error codes for each "expected failure"
Packit ae9e2a
(e.g. `GIT_ENOTFOUND` for files that take a path which might be missing)
Packit ae9e2a
and a generic error code (-1) for all critical or non-specific failures
Packit ae9e2a
(e.g. running out of memory or system corruption).
Packit ae9e2a
Packit ae9e2a
When a negative value is returned, an error message is also set.  The
Packit ae9e2a
message can be accessed via the `giterr_last` function which will return a
Packit ae9e2a
pointer to a `git_error` structure containing the error message text and
Packit ae9e2a
the class of error (i.e. what part of the library generated the error).
Packit ae9e2a
Packit ae9e2a
For instance: An object lookup by SHA prefix (`git_object_lookup_prefix`)
Packit ae9e2a
has two expected failure cases: the SHA is not found at all which returns
Packit ae9e2a
`GIT_ENOTFOUND` or the SHA prefix is ambiguous (i.e. two or more objects
Packit ae9e2a
share the prefix) which returns `GIT_EAMBIGUOUS`.  There are any number of
Packit ae9e2a
critical failures (such as a packfile being corrupted, a loose object
Packit ae9e2a
having the wrong access permissions, etc.) all of which will return -1.
Packit ae9e2a
When the object lookup is successful, it will return 0.
Packit ae9e2a
Packit ae9e2a
If libgit2 was compiled with threads enabled (`-DTHREADSAFE=ON` when using
Packit ae9e2a
CMake), then the error message will be kept in thread-local storage, so it
Packit ae9e2a
will not be modified by other threads.  If threads are not enabled, then
Packit ae9e2a
the error message is in global data.
Packit ae9e2a
Packit ae9e2a
All of the error return codes, the `git_error` type, the error access
Packit ae9e2a
functions, and the error classes are defined in `include/git2/errors.h`.
Packit ae9e2a
See the documentation there for details on the APIs for accessing,
Packit ae9e2a
clearing, and even setting error codes.
Packit ae9e2a
Packit ae9e2a
When writing libgit2 code, please be smart and conservative when returning
Packit ae9e2a
error codes.  Functions usually have a maximum of two or three "expected
Packit ae9e2a
errors" and in most cases only one.  If you feel there are more possible
Packit ae9e2a
expected error scenarios, then the API you are writing may be at too high
Packit ae9e2a
a level for core libgit2.
Packit ae9e2a
Packit ae9e2a
Example usage
Packit ae9e2a
-------------
Packit ae9e2a
Packit ae9e2a
When using libgit2, you will typically capture the return value from
Packit ae9e2a
functions using an `int` variable and check to see if it is negative.
Packit ae9e2a
When that happens, you can, if you wish, look at the specific value or
Packit ae9e2a
look at the error message that was generated.
Packit ae9e2a
Packit ae9e2a
~~~c
Packit ae9e2a
{
Packit ae9e2a
	git_repository *repo;
Packit ae9e2a
	int error = git_repository_open(&repo, "path/to/repo");
Packit ae9e2a
Packit ae9e2a
	if (error < 0) {
Packit ae9e2a
		fprintf(stderr, "Could not open repository: %s\n", giterr_last()->message);
Packit ae9e2a
		exit(1);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	... use `repo` here ...
Packit ae9e2a
Packit ae9e2a
	git_repository_free(repo); /* void function - no error return code */
Packit ae9e2a
}
Packit ae9e2a
~~~
Packit ae9e2a
Packit ae9e2a
Some of the error return values do have meaning.  Optionally, you can look
Packit ae9e2a
at the specific error values to decide what to do.
Packit ae9e2a
Packit ae9e2a
~~~c
Packit ae9e2a
{
Packit ae9e2a
	git_repository *repo;
Packit ae9e2a
	const char *path = "path/to/repo";
Packit ae9e2a
	int error = git_repository_open(&repo, path);
Packit ae9e2a
Packit ae9e2a
	if (error < 0) {
Packit ae9e2a
		if (error == GIT_ENOTFOUND)
Packit ae9e2a
			fprintf(stderr, "Could not find repository at path '%s'\n", path);
Packit ae9e2a
		else
Packit ae9e2a
			fprintf(stderr, "Unable to open repository: %s\n",
Packit ae9e2a
				giterr_last()->message);
Packit ae9e2a
		exit(1);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	... happy ...
Packit ae9e2a
}
Packit ae9e2a
~~~
Packit ae9e2a
Packit ae9e2a
Some of the higher-level language bindings may use a range of information
Packit ae9e2a
from libgit2 to convert error return codes into exceptions, including the
Packit ae9e2a
specific error return codes and even the class of error and the error
Packit ae9e2a
message returned by `giterr_last`, but the full range of that logic is
Packit ae9e2a
beyond the scope of this document.
Packit ae9e2a
Packit ae9e2a
Example internal implementation
Packit ae9e2a
-------------------------------
Packit ae9e2a
Packit ae9e2a
Internally, libgit2 detects error scenarios, records error messages, and
Packit ae9e2a
returns error values.  Errors from low-level functions are generally
Packit ae9e2a
passed upwards (unless the higher level can either handle the error or
Packit ae9e2a
wants to translate the error into something more meaningful).
Packit ae9e2a
Packit ae9e2a
~~~c
Packit ae9e2a
int git_repository_open(git_repository **repository, const char *path)
Packit ae9e2a
{
Packit ae9e2a
	/* perform some logic to open the repository */
Packit ae9e2a
	if (p_exists(path) < 0) {
Packit ae9e2a
		giterr_set(GITERR_REPOSITORY, "The path '%s' doesn't exist", path);
Packit ae9e2a
		return GIT_ENOTFOUND;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	...
Packit ae9e2a
}
Packit ae9e2a
~~~
Packit ae9e2a
Packit ae9e2a
The public error API
Packit ae9e2a
--------------------
Packit ae9e2a
Packit ae9e2a
- `const git_error *giterr_last(void)`: The main function used to look up
Packit ae9e2a
  the last error.  This may return NULL if no error has occurred.
Packit ae9e2a
  Otherwise this should return a `git_error` object indicating the class
Packit ae9e2a
  of error and the error message that was generated by the library.
Packit ae9e2a
Packit ae9e2a
  The last error is stored in thread-local storage when libgit2 is
Packit ae9e2a
  compiled with thread support, so you do not have to worry about another
Packit ae9e2a
  thread overwriting the value.  When thread support is off, the last
Packit ae9e2a
  error is a global value.
Packit ae9e2a
Packit ae9e2a
  _Note_ There are some known bugs in the library where this may return
Packit ae9e2a
  NULL even when an error code was generated.  Please report these as
Packit ae9e2a
  bugs, but in the meantime, please code defensively and check for NULL
Packit ae9e2a
  when calling this function.
Packit ae9e2a
Packit ae9e2a
- `void giterr_clear(void)`: This function clears the last error.  The
Packit ae9e2a
  library will call this when an error is generated by low level function
Packit ae9e2a
  and the higher level function handles the error.
Packit ae9e2a
Packit ae9e2a
  _Note_ There are some known bugs in the library where a low level
Packit ae9e2a
  function's error message is not cleared by higher level code that
Packit ae9e2a
  handles the error and returns zero.  Please report these as bugs, but in
Packit ae9e2a
  the meantime, a zero return value from a libgit2 API does not guarantee
Packit ae9e2a
  that `giterr_last()` will return NULL.
Packit ae9e2a
Packit ae9e2a
- `void giterr_set_str(int error_class, const char *message)`: This
Packit ae9e2a
  function can be used when writing a custom backend module to set the
Packit ae9e2a
  libgit2 error message.  See the documentation on this function for its
Packit ae9e2a
  use.  Normal usage of libgit2 will probably never need to call this API.
Packit ae9e2a
Packit ae9e2a
- `void giterr_set_oom(void)`: This is a standard function for reporting
Packit ae9e2a
  an out-of-memory error.  It is written in a manner that it doesn't have
Packit ae9e2a
  to allocate any extra memory in order to record the error, so this is
Packit ae9e2a
  the best way to report that scenario.
Packit ae9e2a
Packit ae9e2a
Deviations from the standard
Packit ae9e2a
----------------------------
Packit ae9e2a
Packit ae9e2a
There are some public functions that do not return `int` values.  There
Packit ae9e2a
are two primary cases:
Packit ae9e2a
Packit ae9e2a
* `void` return values: If a function has a `void` return, then it will
Packit ae9e2a
  never fail.  This primary will be used for object destructors.
Packit ae9e2a
Packit ae9e2a
* `git_xyz *` return values: These are simple accessor functions where the
Packit ae9e2a
  only meaningful error would typically be looking something up by index
Packit ae9e2a
  and having the index be out of bounds.  In those cases, the function
Packit ae9e2a
  will typically return NULL.
Packit ae9e2a
Packit ae9e2a
* Boolean return values: There are some cases where a function cannot fail
Packit ae9e2a
  and wants to return a boolean value.  In those cases, we try to return 1
Packit ae9e2a
  for true and 0 for false.  These cases are rare and the return value for
Packit ae9e2a
  the function should probably be an `unsigned int` to denote these cases.
Packit ae9e2a
  If you find an exception, please open an issue and let's fix it.
Packit ae9e2a
Packit ae9e2a
There are a few other exceptions to these rules here and there in the
Packit ae9e2a
library, but those are extremely rare and should probably be converted
Packit ae9e2a
over to other to more standard patterns for usage.  Feel free to open
Packit ae9e2a
issues pointing these out.
Packit ae9e2a
Packit ae9e2a
There are some known bugs in the library where some functions may return a
Packit ae9e2a
negative value but not set an error message and some other functions may
Packit ae9e2a
return zero (no error) and yet leave an error message set.  Please report
Packit ae9e2a
these cases as issues and they will be fixed.  In the meanwhile, please
Packit ae9e2a
code defensively, checking that the return value of `giterr_last` is not
Packit ae9e2a
NULL before using it, and not relying on `giterr_last` to return NULL when
Packit ae9e2a
a function returns 0 for success.
Packit ae9e2a
Packit ae9e2a
The internal error API
Packit ae9e2a
----------------------
Packit ae9e2a
Packit ae9e2a
- `void giterr_set(int error_class, const char *fmt, ...)`: This is the
Packit ae9e2a
  main internal function for setting an error.  It works like `printf` to
Packit ae9e2a
  format the error message.  See the notes of `giterr_set_str` for a
Packit ae9e2a
  general description of how error messages are stored (and also about
Packit ae9e2a
  special handling for `error_class` of `GITERR_OS`).
Packit ae9e2a
Packit ae9e2a
Writing error messages
Packit ae9e2a
----------------------
Packit ae9e2a
Packit ae9e2a
Here are some guidelines when writing error messages:
Packit ae9e2a
Packit ae9e2a
- Use proper English, and an impersonal or past tenses: *The given path
Packit ae9e2a
  does not exist*, *Failed to lookup object in ODB*
Packit ae9e2a
Packit ae9e2a
- Use short, direct and objective messages. **One line, max**. libgit2 is
Packit ae9e2a
  a low level library: think that all the messages reported will be thrown
Packit ae9e2a
  as Ruby or Python exceptions. Think how long are common exception
Packit ae9e2a
  messages in those languages.
Packit ae9e2a
Packit ae9e2a
- **Do not add redundant information to the error message**, specially
Packit ae9e2a
  information that can be inferred from the context.
Packit ae9e2a
Packit ae9e2a
	E.g. in `git_repository_open`, do not report a message like "Failed to
Packit ae9e2a
	open repository: path not found". Somebody is calling that
Packit ae9e2a
	function. If it fails, they already know that the repository failed to
Packit ae9e2a
	open!
Packit ae9e2a
Packit ae9e2a
General guidelines for error reporting
Packit ae9e2a
--------------------------------------
Packit ae9e2a
Packit ae9e2a
- Libgit2 does not handle programming errors with these
Packit ae9e2a
  functions. Programming errors are `assert`ed, and when their source is
Packit ae9e2a
  internal, fixed as soon as possible. This is C, people.
Packit ae9e2a
Packit ae9e2a
	Example of programming errors that would **not** be handled: passing
Packit ae9e2a
    NULL to a function that expects a valid pointer; passing a `git_tree`
Packit ae9e2a
    to a function that expects a `git_commit`. All these cases need to be
Packit ae9e2a
    identified with `assert` and fixed asap.
Packit ae9e2a
Packit ae9e2a
	Example of a runtime error: failing to parse a `git_tree` because it
Packit ae9e2a
    contains invalid data. Failing to open a file because it doesn't exist
Packit ae9e2a
    on disk. These errors are handled, a meaningful error message is set,
Packit ae9e2a
    and an error code is returned.
Packit ae9e2a
Packit ae9e2a
- In general, *do not* try to overwrite errors internally and *do*
Packit ae9e2a
  propagate error codes from lower level functions to the higher level.
Packit ae9e2a
  There are some cases where propagating an error code will be more
Packit ae9e2a
  confusing rather than less, so there are some exceptions to this rule,
Packit ae9e2a
  but the default behavior should be to simply clean up and pass the error
Packit ae9e2a
  on up to the caller.
Packit ae9e2a
Packit ae9e2a
    **WRONG**
Packit ae9e2a
Packit ae9e2a
	~~~c
Packit ae9e2a
	int git_commit_parent(...)
Packit ae9e2a
	{
Packit ae9e2a
		...
Packit ae9e2a
Packit ae9e2a
		if (git_commit_lookup(parent, repo, parent_id) < 0) {
Packit ae9e2a
			giterr_set(GITERR_COMMIT, "Overwrite lookup error message");
Packit ae9e2a
			return -1; /* mask error code */
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		...
Packit ae9e2a
	}
Packit ae9e2a
	~~~
Packit ae9e2a
Packit ae9e2a
	**RIGHT**
Packit ae9e2a
Packit ae9e2a
	~~~c
Packit ae9e2a
	int git_commit_parent(...)
Packit ae9e2a
	{
Packit ae9e2a
		...
Packit ae9e2a
Packit ae9e2a
		error = git_commit_lookup(parent, repo, parent_id);
Packit ae9e2a
		if (error < 0) {
Packit ae9e2a
			/* cleanup intermediate objects if necessary */
Packit ae9e2a
			/* leave error message and propagate error code */
Packit ae9e2a
			return error;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		...
Packit ae9e2a
	}
Packit ae9e2a
	~~~