summaryrefslogtreecommitdiff
path: root/src/common.h
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-12-03 16:45:39 -0800
committerRussell Belfer <rb@github.com>2013-12-11 10:57:49 -0800
commit96869a4edb2872934e0e167a726ab240f4270fea (patch)
tree2d770414acef2d1d45a609e004c0aa6fa56d06d7 /src/common.h
parent9f77b3f6f5ce6944ec49dfc666ef6b8df0af0c6b (diff)
downloadlibgit2-96869a4edb2872934e0e167a726ab240f4270fea.tar.gz
Improve GIT_EUSER handling
This adds giterr_user_cancel to return GIT_EUSER and clear any error message that is sitting around. As a result of using that in places, we need to be more thorough with capturing errors that happen inside a callback when used internally. To help with that, this also adds giterr_capture and giterr_restore so that when we internally use a foreach-type function that clears errors and converts them to GIT_EUSER, it is easier to restore not just the return value, but the actual error message text.
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/common.h b/src/common.h
index a1888785e..0ad4130aa 100644
--- a/src/common.h
+++ b/src/common.h
@@ -78,28 +78,42 @@ int giterr_set_regex(const regex_t *regex, int error_code);
/**
* Gets the system error code for this thread.
*/
-GIT_INLINE(int) giterr_system_last(void)
-{
-#ifdef GIT_WIN32
- return GetLastError();
-#else
- return errno;
-#endif
-}
+int giterr_system_last(void);
/**
* Sets the system error code for this thread.
*/
-GIT_INLINE(void) giterr_system_set(int code)
+void giterr_system_set(int code);
+
+/**
+ * Note that a user cancelled an operation with GIT_EUSER
+ */
+GIT_INLINE(int) giterr_user_cancel(void)
{
-#ifdef GIT_WIN32
- SetLastError(code);
-#else
- errno = code;
-#endif
+ giterr_clear();
+ return GIT_EUSER;
}
/**
+ * 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
*/
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)