Skip to content

Commit

Permalink
Merge pull request libgit2#6625 from libgit2/ethomson/error_updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ethomson authored Sep 5, 2023
2 parents f51e70d + 5a63b43 commit 4939fa7
Show file tree
Hide file tree
Showing 21 changed files with 627 additions and 576 deletions.
63 changes: 11 additions & 52 deletions include/git2/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,63 +118,22 @@ typedef enum {
* Return the last `git_error` object that was generated for the
* current thread.
*
* The default behaviour of this function is to return NULL if no previous error has occurred.
* However, libgit2's error strings are not cleared aggressively, so a prior
* (unrelated) error may be returned. This can be avoided by only calling
* this function if the prior call to a libgit2 API returned an error.
* This function will never return NULL.
*
* @return A git_error object.
*/
GIT_EXTERN(const git_error *) git_error_last(void);

/**
* Clear the last library error that occurred for this thread.
*/
GIT_EXTERN(void) git_error_clear(void);

/**
* Set the error message string for this thread, using `printf`-style
* formatting.
*
* This function is public so that custom ODB backends and the like can
* relay an error message through libgit2. Most regular users of libgit2
* will never need to call this function -- actually, calling it in most
* circumstances (for example, calling from within a callback function)
* will just end up having the value overwritten by libgit2 internals.
* Callers should not rely on this to determine whether an error has
* occurred. For error checking, callers should examine the return
* codes of libgit2 functions.
*
* This error message is stored in thread-local storage and only applies
* to the particular thread that this libgit2 call is made from.
* This call can only reliably report error messages when an error
* has occurred. (It may contain stale information if it is called
* after a different function that succeeds.)
*
* @param error_class One of the `git_error_t` enum above describing the
* general subsystem that is responsible for the error.
* @param fmt The `printf`-style format string; subsequent arguments must
* be the arguments for the format string.
*/
GIT_EXTERN(void) git_error_set(int error_class, const char *fmt, ...)
GIT_FORMAT_PRINTF(2, 3);

/**
* Set the error message string for this thread. This function is like
* `git_error_set` but takes a static string instead of a `printf`-style
* format.
* The memory for this object is managed by libgit2. It should not
* be freed.
*
* @param error_class One of the `git_error_t` enum above describing the
* general subsystem that is responsible for the error.
* @param string The error message to keep
* @return 0 on success or -1 on failure
*/
GIT_EXTERN(int) git_error_set_str(int error_class, const char *string);

/**
* Set the error message to a special value for memory allocation failure.
*
* The normal `git_error_set_str()` function attempts to `strdup()` the
* string that is passed in. This is not a good idea when the error in
* question is a memory allocation failure. That circumstance has a
* special setter function that sets the error string to a known and
* statically allocated internal value.
* @return A git_error object.
*/
GIT_EXTERN(void) git_error_set_oom(void);
GIT_EXTERN(const git_error *) git_error_last(void);

/** @} */
GIT_END_DECL
Expand Down
66 changes: 66 additions & 0 deletions include/git2/sys/errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/

#ifndef INCLUDE_sys_git_errors_h__
#define INCLUDE_sys_git_errors_h__

#include "git2/common.h"

GIT_BEGIN_DECL

/**
* Clear the last library error that occurred for this thread.
*/
GIT_EXTERN(void) git_error_clear(void);

/**
* Set the error message string for this thread, using `printf`-style
* formatting.
*
* This function is public so that custom ODB backends and the like can
* relay an error message through libgit2. Most regular users of libgit2
* will never need to call this function -- actually, calling it in most
* circumstances (for example, calling from within a callback function)
* will just end up having the value overwritten by libgit2 internals.
*
* This error message is stored in thread-local storage and only applies
* to the particular thread that this libgit2 call is made from.
*
* @param error_class One of the `git_error_t` enum above describing the
* general subsystem that is responsible for the error.
* @param fmt The `printf`-style format string; subsequent arguments must
* be the arguments for the format string.
*/
GIT_EXTERN(void) git_error_set(int error_class, const char *fmt, ...)
GIT_FORMAT_PRINTF(2, 3);

/**
* Set the error message string for this thread. This function is like
* `git_error_set` but takes a static string instead of a `printf`-style
* format.
*
* @param error_class One of the `git_error_t` enum above describing the
* general subsystem that is responsible for the error.
* @param string The error message to keep
* @return 0 on success or -1 on failure
*/
GIT_EXTERN(int) git_error_set_str(int error_class, const char *string);

/**
* Set the error message to a special value for memory allocation failure.
*
* The normal `git_error_set_str()` function attempts to `strdup()` the
* string that is passed in. This is not a good idea when the error in
* question is a memory allocation failure. That circumstance has a
* special setter function that sets the error string to a known and
* statically allocated internal value.
*/
GIT_EXTERN(void) git_error_set_oom(void);

GIT_END_DECL

#endif
6 changes: 3 additions & 3 deletions src/libgit2/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,15 @@ static int git__clone(
}

if (error != 0) {
git_error_state last_error = {0};
git_error_state_capture(&last_error, error);
git_error *last_error;
git_error_save(&last_error);

git_repository_free(repo);
repo = NULL;

(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);

git_error_state_restore(&last_error);
git_error_restore(last_error);
}

*out = repo;
Expand Down
Loading

0 comments on commit 4939fa7

Please sign in to comment.