2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
14 /********************************************
16 ********************************************/
18 static git_error g_git_oom_error
= {
23 static void set_error_from_buffer(int error_class
)
25 git_error
*error
= &GIT_GLOBAL
->error_t
;
26 git_buf
*buf
= &GIT_GLOBAL
->error_buf
;
28 error
->message
= buf
->ptr
;
29 error
->klass
= error_class
;
31 GIT_GLOBAL
->last_error
= error
;
34 static void set_error(int error_class
, char *string
)
36 git_buf
*buf
= &GIT_GLOBAL
->error_buf
;
40 git_buf_puts(buf
, string
);
44 set_error_from_buffer(error_class
);
47 void git_error_set_oom(void)
49 GIT_GLOBAL
->last_error
= &g_git_oom_error
;
52 void git_error_set(int error_class
, const char *string
, ...)
56 DWORD win32_error_code
= (error_class
== GIT_ERROR_OS
) ? GetLastError() : 0;
58 int error_code
= (error_class
== GIT_ERROR_OS
) ? errno
: 0;
59 git_buf
*buf
= &GIT_GLOBAL
->error_buf
;
63 va_start(arglist
, string
);
64 git_buf_vprintf(buf
, string
, arglist
);
67 if (error_class
== GIT_ERROR_OS
)
68 git_buf_PUTS(buf
, ": ");
71 if (error_class
== GIT_ERROR_OS
) {
73 char * win32_error
= git_win32_get_error_message(win32_error_code
);
75 git_buf_puts(buf
, win32_error
);
76 git__free(win32_error
);
83 git_buf_puts(buf
, strerror(error_code
));
89 if (!git_buf_oom(buf
))
90 set_error_from_buffer(error_class
);
93 void git_error_set_str(int error_class
, const char *string
)
95 git_buf
*buf
= &GIT_GLOBAL
->error_buf
;
103 git_buf_puts(buf
, string
);
104 if (!git_buf_oom(buf
))
105 set_error_from_buffer(error_class
);
108 int git_error_set_regex(const regex_t
*regex
, int error_code
)
110 char error_buf
[1024];
114 regerror(error_code
, regex
, error_buf
, sizeof(error_buf
));
115 git_error_set_str(GIT_ERROR_REGEX
, error_buf
);
117 if (error_code
== REG_NOMATCH
)
118 return GIT_ENOTFOUND
;
120 return GIT_EINVALIDSPEC
;
123 void git_error_clear(void)
125 if (GIT_GLOBAL
->last_error
!= NULL
) {
127 GIT_GLOBAL
->last_error
= NULL
;
136 const git_error
*git_error_last(void)
138 return GIT_GLOBAL
->last_error
;
141 int git_error_state_capture(git_error_state
*state
, int error_code
)
143 git_error
*error
= GIT_GLOBAL
->last_error
;
144 git_buf
*error_buf
= &GIT_GLOBAL
->error_buf
;
146 memset(state
, 0, sizeof(git_error_state
));
151 state
->error_code
= error_code
;
152 state
->oom
= (error
== &g_git_oom_error
);
155 state
->error_msg
.klass
= error
->klass
;
158 state
->error_msg
.message
= g_git_oom_error
.message
;
160 state
->error_msg
.message
= git_buf_detach(error_buf
);
167 int git_error_state_restore(git_error_state
*state
)
173 if (state
&& state
->error_msg
.message
) {
177 set_error(state
->error_msg
.klass
, state
->error_msg
.message
);
179 ret
= state
->error_code
;
180 memset(state
, 0, sizeof(git_error_state
));
186 void git_error_state_free(git_error_state
*state
)
192 git__free(state
->error_msg
.message
);
194 memset(state
, 0, sizeof(git_error_state
));
197 int git_error_system_last(void)
200 return GetLastError();
206 void git_error_system_set(int code
)
215 /* Deprecated error values and functions */
217 const git_error
*giterr_last(void)
219 return git_error_last();
222 void giterr_clear(void)
227 void giterr_set_str(int error_class
, const char *string
)
229 git_error_set_str(error_class
, string
);
232 void giterr_set_oom(void)