diff options
| author | Vicent Marti <vicent@github.com> | 2013-12-13 06:20:19 -0800 |
|---|---|---|
| committer | Vicent Marti <vicent@github.com> | 2013-12-13 06:20:19 -0800 |
| commit | 79194bcdc956406979cd27ac99198826860d3f20 (patch) | |
| tree | 407594b97b6e39ef3ac0723dc33aa3162ce933cc /src/common.h | |
| parent | 25a1fab0a96fd87e4ebc4ec195ac59a4213e92ad (diff) | |
| parent | 7a16d54b5457aa9f60c25a204277ae0ce609ad2e (diff) | |
| download | libgit2-79194bcdc956406979cd27ac99198826860d3f20.tar.gz | |
Merge pull request #1986 from libgit2/rb/error-handling-cleanups
Clean up some error handling and change callback error behavior
Diffstat (limited to 'src/common.h')
| -rw-r--r-- | src/common.h | 61 |
1 files changed, 47 insertions, 14 deletions
diff --git a/src/common.h b/src/common.h index a1888785e..e315b5979 100644 --- a/src/common.h +++ b/src/common.h @@ -62,7 +62,7 @@ * Check a return value and propogate result if non-zero. */ #define GITERR_CHECK_ERROR(code) \ - do { int _err = (code); if (_err < 0) return _err; } while (0) + do { int _err = (code); if (_err) return _err; } while (0) /** * Set the error message for this thread, formatting as needed. @@ -76,28 +76,61 @@ void giterr_set(int error_class, const char *string, ...); int giterr_set_regex(const regex_t *regex, int error_code); /** - * Gets the system error code for this thread. + * Set error message for user callback if needed. + * + * If the error code in non-zero and no error message is set, this + * sets a generic error message. + * + * @return This always returns the `error_code` parameter. */ -GIT_INLINE(int) giterr_system_last(void) +GIT_INLINE(int) giterr_set_after_callback_function( + int error_code, const char *action) { + if (error_code) { + const git_error *e = giterr_last(); + if (!e || !e->message) + giterr_set(e ? e->klass : GITERR_CALLBACK, + "%s callback returned %d", action, error_code); + } + return error_code; +} + #ifdef GIT_WIN32 - return GetLastError(); +#define giterr_set_after_callback(code) \ + giterr_set_after_callback_function((code), __FUNCTION__) #else - return errno; +#define giterr_set_after_callback(code) \ + giterr_set_after_callback_function((code), __func__) #endif -} + +/** + * Gets the system error code for this thread. + */ +int giterr_system_last(void); /** * Sets the system error code for this thread. */ -GIT_INLINE(void) giterr_system_set(int code) -{ -#ifdef GIT_WIN32 - SetLastError(code); -#else - errno = code; -#endif -} +void giterr_system_set(int code); + +/** + * Structure to preserve libgit2 error state + */ +typedef struct { + int error_code; + git_error error_msg; +} git_error_state; + +/** + * Capture current error state to restore later, returning error code. + * If `error_code` is zero, this does nothing and returns zero. + */ +int giterr_capture(git_error_state *state, int error_code); + +/** + * Restore error state to a previous value, returning saved error code. + */ +int giterr_restore(git_error_state *state); /** * Check a versioned structure for validity |
