PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/error-handling.md

https://github.com/kdh8544/libgit2
Markdown | 111 lines | 73 code | 38 blank | 0 comment | 0 complexity | 53aa5f6e15505d025e688ab28877476e MD5 | raw file
Possible License(s): LGPL-2.1, MIT
  1. Error reporting in libgit2
  2. ==========================
  3. Error reporting is performed on an explicit `git_error **` argument, which appears at the end of all API calls that can return an error. Yes, this does clutter the API.
  4. When a function fails, an error is set on the error variable **and** returns one of the generic error codes.
  5. ~~~c
  6. int git_repository_open(git_repository **repository, const char *path, git_error **error)
  7. {
  8. // perform some opening
  9. if (p_exists(path) < 0) {
  10. giterr_set(error, GITERR_REPOSITORY, "The path '%s' doesn't exist", path);
  11. return GIT_ENOTFOUND;
  12. }
  13. ...
  14. if (try_to_parse(path, error) < 0)
  15. return GIT_ERROR;
  16. ...
  17. }
  18. ~~~
  19. The simple error API
  20. --------------------
  21. - `void giterr_set(git_error **, int, const char *, ...)`: the main function used to set an error. It allocates a new error object and stores it in the passed error pointer. It has no return value. The arguments for `giterr_set` are as follows:
  22. - `git_error **error_ptr`: the pointer where the error will be created.
  23. - `int error_class`: the class for the error. This is **not** an error code: this is an specific enum that specifies the error family. The point is to map these families 1-1 with Exception types on higher level languages (e.g. GitRepositoryException)
  24. - `const char *error_str, ...`: the error string, with optional formatting arguments
  25. - `void giterr_free(git_error *)`: takes an error and frees it. This function is available in the external API.
  26. - `void giterr_clear(git_error **)`: clears an error previously set in an error pointer, setting it to NULL and calling `giterr_free` on it.
  27. - `void giterr_propagate(git_error **, git_error *)`: moves an error to a given error pointer, handling the case when the error pointer is NULL (in that case the error gets freed, because it cannot be propagated).
  28. The new error code return values
  29. --------------------------------
  30. We are doing this the POSIX way: one error code for each "expected failure", and a generic error code for all the critical failures.
  31. For instance: A reference lookup can have an expected failure (which is when the reference cannot be found), and a critical failure (which could be any of a long list of things that could go wrong, such as the refs packfile being corrupted, a loose ref being written with the wrong permissions, etc). We cannot have distinct error codes for every single error in the library, hence `git_reference_lookup` would return GIT_SUCCESS if the operation was successful, GIT_ENOTFOUND when the reference doesn't exist, and GIT_ERROR when an error happens -- **the error is then detailed in the `git_error` parameter**.
  32. Please be smart when returning error codes. Functions have max two "expected errors", and in most cases only one.
  33. Writing error messages
  34. ----------------------
  35. Here are some guidelines when writing error messages:
  36. - Use proper English, and an impersonal or past tenses: *The given path does not exist*, *Failed to lookup object in ODB*
  37. - Use short, direct and objective messages. **One line, max**. libgit2 is a low level library: think that all the messages reported will be thrown as Ruby or Python exceptions. Think how long are common exception messages in those languages.
  38. - **Do not add redundant information to the error message**, specially information that can be inferred from the context.
  39. E.g. in `git_repository_open`, do not report a message like "Failed to open repository: path not found". Somebody is
  40. calling that function. If it fails, he already knows that the repository failed to open!
  41. General guidelines for error reporting
  42. --------------------------------------
  43. - We never handle programming errors with these functions. Programming errors are `assert`ed, and when their source is internal, fixed as soon as possible. This is C, people.
  44. Example of programming errors that would **not** be handled: passing NULL to a function that expects a valid pointer; passing a `git_tree` to a function that expects a `git_commit`. All these cases need to be identified with `assert` and fixed asap.
  45. Example of a runtime error: failing to parse a `git_tree` because it contains invalid data. Failing to open a file because it doesn't exist on disk. These errors would be handled, and a `git_error` would be set.
  46. - The `git_error **` argument is always the last in the signature of all API calls. No exceptions.
  47. - When the programmer (or us, internally) doesn't need error handling, he can pass `NULL` to the `git_error **` param. This means that the errors won't be *reported*, but obviously they still will be handled (i.e. the failing function will interrupt and return cleanly). This is transparently handled by `giterr_set`
  48. - `git_error *` **must be initialized to `NULL` before passing its value to a function!!**
  49. ~~~c
  50. git_error *err;
  51. git_error *good_error = NULL;
  52. git_foo_func(arg1, arg2, &error); // invalid: `error` is not initialized
  53. git_foo_func2(arg1, arg2, &good_error); // OK!
  54. git_foo_func3(arg1, arg2, NULL); // OK! But no error reporting!
  55. ~~~
  56. - Piling up errors is an error! Don't do this! Errors must always be free'd when a function returns.
  57. ~~~c
  58. git_error *error = NULL;
  59. git_foo_func1(arg1, &error);
  60. git_foo_func2(arg2, &error); // WRONG! What if func1 failed? `error` would leak!
  61. ~~~
  62. - Likewise: do not rethrow errors internally!
  63. ~~~c
  64. int git_commit_create(..., git_error **error)
  65. {
  66. if (git_reference_exists("HEAD", error) < 0) {
  67. /* HEAD does not exist; create it so we can commit... */
  68. if (git_reference_create("HEAD", error) < 0) {
  69. /* error could be rethrown */
  70. }
  71. }
  72. - Remember that errors are now allocated, and hence they need to be free'd after they've been used. Failure to do so internally (e.g. in the already seen examples of error piling) will be reported by Valgrind, so we can easily find where are we rethrowing errors.
  73. - Remember that any function that fails **will set an error object**, and that object will be freed.