diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-04-06 10:33:28 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-06-05 07:14:24 +0100 |
commit | bfa4bcf892e31243abf2ad7b7d784e6eb22eba3b (patch) | |
tree | 02c7c810ea2a42cfda2d85216d49600089f5ab19 | |
parent | dc30898c6e460738b58352a73f9d1445359036ea (diff) | |
download | libgit2-bfa4bcf892e31243abf2ad7b7d784e6eb22eba3b.tar.gz |
errors: move system error message getter to util
Provide the system error message functionality (strerror or the string
handling for GetLastError on Windows) into a utility class.
-rw-r--r-- | src/buffer.c | 30 | ||||
-rw-r--r-- | src/buffer.h | 3 | ||||
-rw-r--r-- | src/errors.c | 23 |
3 files changed, 35 insertions, 21 deletions
diff --git a/src/buffer.c b/src/buffer.c index ce97ccffa..e50b40977 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1080,3 +1080,33 @@ int git_buf_getenv(git_buf *out, const char *name) return git_buf_puts(out, val); } #endif /* GIT_WIN32 */ + +int git_buf_system_errmsg(git_buf *out) +{ +#ifdef GIT_WIN32 + if (GetLastError() != 0) { + char *msg = git_win32_get_error_message(GetLastError()); + + if (msg == NULL) { + out->ptr = git_buf__oom; + return -1; + } + + git_buf_puts(out, msg); + git__free(msg); + + SetLastError(0); + return 0; + } +#endif + + if (errno) { + git_buf_puts(out, strerror(errno)); + + errno = 0; + return 0; + } + + git_buf_puts(out, "no error"); + return 0; +} diff --git a/src/buffer.h b/src/buffer.h index e2023cc59..c845c7de5 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -229,6 +229,9 @@ int git_buf_decode_percent(git_buf *buf, const char *str, size_t len); /* Place the value of the given environment variable into the buffer */ int git_buf_getenv(git_buf *buf, const char *name); +/* Place the value of the current system error message into the buffer */ +int git_buf_system_errmsg(git_buf *buf); + /* * Insert, remove or replace a portion of the buffer. * diff --git a/src/errors.c b/src/errors.c index b34aa3abb..dfb5e3c4f 100644 --- a/src/errors.c +++ b/src/errors.c @@ -60,10 +60,6 @@ void git_error_set(int error_class, const char *fmt, ...) void git_error_vset(int error_class, const char *fmt, va_list ap) { -#ifdef GIT_WIN32 - DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0; -#endif - int error_code = (error_class == GIT_ERROR_OS) ? errno : 0; git_buf *buf = &GIT_GLOBAL->error_buf; git_buf_clear(buf); @@ -73,23 +69,8 @@ void git_error_vset(int error_class, const char *fmt, va_list ap) git_buf_PUTS(buf, ": "); } - if (error_class == GIT_ERROR_OS) { -#ifdef GIT_WIN32 - char * win32_error = git_win32_get_error_message(win32_error_code); - if (win32_error) { - git_buf_puts(buf, win32_error); - git__free(win32_error); - - SetLastError(0); - } - else -#endif - if (error_code) - git_buf_puts(buf, strerror(error_code)); - - if (error_code) - errno = 0; - } + if (error_class == GIT_ERROR_OS) + git_buf_system_errmsg(buf); if (!git_buf_oom(buf)) set_error_from_buffer(error_class); |