]> git.proxmox.com Git - libgit2.git/blame - src/errors.c
buffer: Add `git_buf_vprintf`
[libgit2.git] / src / errors.c
CommitLineData
02f9e637 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
02f9e637 3 *
bb742ede
VM
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
02f9e637 6 */
ae234862 7#include "common.h"
a15c550d 8#include "global.h"
60bc2d20 9#include "posix.h"
02f9e637
VM
10#include <stdarg.h>
11
60bc2d20
VM
12/********************************************
13 * New error handling
14 ********************************************/
15
1a481123
VM
16static git_error g_git_oom_error = {
17 "Out of memory",
18 GITERR_NOMEMORY
19};
20
21void giterr_set_oom(void)
22{
23 GIT_GLOBAL->last_error = &g_git_oom_error;
24}
25
26void giterr_set(int error_class, const char *string, ...)
60bc2d20
VM
27{
28 char error_str[1024];
29 va_list arglist;
60bc2d20 30
e3c47510
RB
31 /* Grab errno before calling vsnprintf() so it won't be overwritten */
32 const char *os_error_msg =
33 (error_class == GITERR_OS && errno != 0) ? strerror(errno) : NULL;
34#ifdef GIT_WIN32
35 DWORD dwLastError = GetLastError();
36#endif
37
60bc2d20
VM
38 va_start(arglist, string);
39 p_vsnprintf(error_str, sizeof(error_str), string, arglist);
40 va_end(arglist);
41
ae9e29fd 42 /* automatically suffix strerror(errno) for GITERR_OS errors */
dda708e7 43 if (error_class == GITERR_OS) {
e3c47510 44 if (os_error_msg != NULL) {
e1de726c 45 strncat(error_str, ": ", sizeof(error_str));
e3c47510
RB
46 strncat(error_str, os_error_msg, sizeof(error_str));
47 errno = 0; /* reset so same error won't be reported twice */
e1de726c
RB
48 }
49#ifdef GIT_WIN32
e3c47510
RB
50 else if (dwLastError != 0) {
51 LPVOID lpMsgBuf = NULL;
e1de726c
RB
52
53 FormatMessage(
54 FORMAT_MESSAGE_ALLOCATE_BUFFER |
55 FORMAT_MESSAGE_FROM_SYSTEM |
56 FORMAT_MESSAGE_IGNORE_INSERTS,
e3c47510 57 NULL, dwLastError, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
e1de726c
RB
58
59 if (lpMsgBuf) {
60 strncat(error_str, ": ", sizeof(error_str));
61 strncat(error_str, (const char *)lpMsgBuf, sizeof(error_str));
62 LocalFree(lpMsgBuf);
63 }
e3c47510
RB
64
65 SetLastError(0);
e1de726c
RB
66 }
67#endif
ae9e29fd
RB
68 }
69
dda708e7
VM
70 giterr_set_str(error_class, error_str);
71}
72
73void giterr_set_str(int error_class, const char *string)
74{
75 git_error *error = &GIT_GLOBAL->error_t;
76
2bc8fa02 77 git__free(error->message);
dda708e7
VM
78
79 error->message = git__strdup(string);
60bc2d20
VM
80 error->klass = error_class;
81
82 if (error->message == NULL) {
1a481123 83 giterr_set_oom();
60bc2d20
VM
84 return;
85 }
86
1a481123 87 GIT_GLOBAL->last_error = error;
60bc2d20
VM
88}
89
dda708e7
VM
90void giterr_set_regex(const regex_t *regex, int error_code)
91{
92 char error_buf[1024];
93 regerror(error_code, regex, error_buf, sizeof(error_buf));
94 giterr_set_str(GITERR_REGEX, error_buf);
95}
96
1a481123 97void giterr_clear(void)
60bc2d20 98{
1a481123 99 GIT_GLOBAL->last_error = NULL;
60bc2d20 100}
e3c47510 101
3fbcac89 102const git_error *giterr_last(void)
e3c47510
RB
103{
104 return GIT_GLOBAL->last_error;
105}
106