From dda708e78f3c3f43d814d46c29ab9f2b9d47ed5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= Date: Fri, 9 Mar 2012 19:55:50 +0100 Subject: error-handling: On-disk config file backend Includes: - Proper error reporting when encountering syntax errors in a config file (file, line number, column). - Rewritten `config_write`, now with 99% less goto-spaghetti - Error state in `git_filebuf`: filebuf write functions no longer need to be checked for error returns. If any of the writes performed on a buffer fail, the last call to `git_filebuf_commit` or `git_filebuf_hash` will fail accordingly and set the appropiate error message. Baller! --- src/errors.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/errors.c') diff --git a/src/errors.c b/src/errors.c index c25fa751..6fb7777f 100644 --- a/src/errors.c +++ b/src/errors.c @@ -122,25 +122,28 @@ void giterr_set(int error_class, const char *string, ...) { char error_str[1024]; va_list arglist; - git_error *error; - const char *oserr = - (error_class == GITERR_OS && errno != 0) ? strerror(errno) : NULL; - - error = &GIT_GLOBAL->error_t; - free(error->message); va_start(arglist, string); p_vsnprintf(error_str, sizeof(error_str), string, arglist); va_end(arglist); /* automatically suffix strerror(errno) for GITERR_OS errors */ - if (oserr != NULL) { + if (error_class == GITERR_OS) { strncat(error_str, ": ", sizeof(error_str)); - strncat(error_str, oserr, sizeof(error_str)); + strncat(error_str, strerror(errno), sizeof(error_str)); errno = 0; } - error->message = git__strdup(error_str); + giterr_set_str(error_class, error_str); +} + +void giterr_set_str(int error_class, const char *string) +{ + git_error *error = &GIT_GLOBAL->error_t; + + free(error->message); + + error->message = git__strdup(string); error->klass = error_class; if (error->message == NULL) { @@ -151,6 +154,13 @@ void giterr_set(int error_class, const char *string, ...) GIT_GLOBAL->last_error = error; } +void giterr_set_regex(const regex_t *regex, int error_code) +{ + char error_buf[1024]; + regerror(error_code, regex, error_buf, sizeof(error_buf)); + giterr_set_str(GITERR_REGEX, error_buf); +} + void giterr_clear(void) { GIT_GLOBAL->last_error = NULL; -- cgit v1.2.1