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 *fmt
, ...)
57 git_error_vset(error_class
, fmt
, ap
);
61 void git_error_vset(int error_class
, const char *fmt
, va_list ap
)
64 DWORD win32_error_code
= (error_class
== GIT_ERROR_OS
) ? GetLastError() : 0;
66 int error_code
= (error_class
== GIT_ERROR_OS
) ? errno
: 0;
67 git_buf
*buf
= &GIT_GLOBAL
->error_buf
;
71 git_buf_vprintf(buf
, fmt
, ap
);
72 if (error_class
== GIT_ERROR_OS
)
73 git_buf_PUTS(buf
, ": ");
76 if (error_class
== GIT_ERROR_OS
) {
78 char * win32_error
= git_win32_get_error_message(win32_error_code
);
80 git_buf_puts(buf
, win32_error
);
81 git__free(win32_error
);
88 git_buf_puts(buf
, strerror(error_code
));
94 if (!git_buf_oom(buf
))
95 set_error_from_buffer(error_class
);
98 int git_error_set_str(int error_class
, const char *string
)
100 git_buf
*buf
= &GIT_GLOBAL
->error_buf
;
105 git_error_set(GIT_ERROR_INVALID
, "unspecified caller error");
110 git_buf_puts(buf
, string
);
112 if (git_buf_oom(buf
))
115 set_error_from_buffer(error_class
);
119 void git_error_clear(void)
121 if (GIT_GLOBAL
->last_error
!= NULL
) {
123 GIT_GLOBAL
->last_error
= NULL
;
132 const git_error
*git_error_last(void)
134 return GIT_GLOBAL
->last_error
;
137 int git_error_state_capture(git_error_state
*state
, int error_code
)
139 git_error
*error
= GIT_GLOBAL
->last_error
;
140 git_buf
*error_buf
= &GIT_GLOBAL
->error_buf
;
142 memset(state
, 0, sizeof(git_error_state
));
147 state
->error_code
= error_code
;
148 state
->oom
= (error
== &g_git_oom_error
);
151 state
->error_msg
.klass
= error
->klass
;
154 state
->error_msg
.message
= g_git_oom_error
.message
;
156 state
->error_msg
.message
= git_buf_detach(error_buf
);
163 int git_error_state_restore(git_error_state
*state
)
169 if (state
&& state
->error_msg
.message
) {
173 set_error(state
->error_msg
.klass
, state
->error_msg
.message
);
175 ret
= state
->error_code
;
176 memset(state
, 0, sizeof(git_error_state
));
182 void git_error_state_free(git_error_state
*state
)
188 git__free(state
->error_msg
.message
);
190 memset(state
, 0, sizeof(git_error_state
));
193 int git_error_system_last(void)
196 return GetLastError();
202 void git_error_system_set(int code
)
211 /* Deprecated error values and functions */
213 const git_error
*giterr_last(void)
215 return git_error_last();
218 void giterr_clear(void)
223 void giterr_set_str(int error_class
, const char *string
)
225 git_error_set_str(error_class
, string
);
228 void giterr_set_oom(void)