diff options
author | Vicent Martà <tanoku@gmail.com> | 2012-02-14 21:23:11 +0100 |
---|---|---|
committer | Vicent Martà <tanoku@gmail.com> | 2012-03-03 02:28:00 +0100 |
commit | 60bc2d20c40e37e92e4e15479ac4dccbde069bec (patch) | |
tree | 0c64df89b9cd66090e2c1665a9a21b19a7113a57 /src/errors.c | |
parent | 845f8314e4a02cbd01b2e7d8a6d608d9e9a1334d (diff) | |
download | libgit2-60bc2d20c40e37e92e4e15479ac4dccbde069bec.tar.gz |
error-handling: Add new routines
Obviously all the old throw routines are still in place, so we can
gradually port over.
Diffstat (limited to 'src/errors.c')
-rw-r--r-- | src/errors.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/errors.c b/src/errors.c index 58e0976f2..0105c2538 100644 --- a/src/errors.c +++ b/src/errors.c @@ -6,6 +6,7 @@ */ #include "common.h" #include "global.h" +#include "posix.h" #include <stdarg.h> static struct { @@ -102,3 +103,66 @@ void git_clearerror(void) char *last_error = GIT_GLOBAL->error.last; last_error[0] = '\0'; } + +/******************************************** + * New error handling + ********************************************/ + +void giterr_set(git_error **error_out, int error_class, const char *string, ...) +{ + char error_str[1024]; + va_list arglist; + git_error *error; + + if (error_out == NULL) + return; + + error = git__malloc(sizeof(git_error)); + if (!error) { + giterr_set_oom(error_out); + return; + } + + va_start(arglist, string); + p_vsnprintf(error_str, sizeof(error_str), string, arglist); + va_end(arglist); + + error->message = git__strdup(error_str); + error->klass = error_class; + + if (error->message == NULL) { + free(error); + giterr_set_oom(error_out); + return; + } + + *error_out = error; +} + +static git_error g_git_oom_error = { + "Out of memory", + GITERR_NOMEMORY +}; + +void giterr_set_oom(git_error **error) +{ + if (error != NULL) + *error = &g_git_oom_error; +} + +void giterr_free(git_error *error) +{ + if (error == NULL || error == &g_git_oom_error) + return; + + free(error->message); + free(error); +} + +void giterr_clear(git_error **error) +{ + if (error != NULL) { + giterr_free(*error); + *error = NULL; + } +} |