2 * Copyright (C) 2009-2012 the libgit2 contributors
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.
16 {GIT_ERROR
, "Unspecified error"},
17 {GIT_ENOTOID
, "Input was not a properly formatted Git object id."},
18 {GIT_ENOTFOUND
, "Object does not exist in the scope searched."},
19 {GIT_ENOMEM
, "Not enough space available."},
20 {GIT_EOSERR
, "Consult the OS error information."},
21 {GIT_EOBJTYPE
, "The specified object is of invalid type"},
22 {GIT_EOBJCORRUPTED
, "The specified object has its data corrupted"},
23 {GIT_ENOTAREPO
, "The specified repository is invalid"},
24 {GIT_EINVALIDTYPE
, "The object or config variable type is invalid or doesn't match"},
25 {GIT_EMISSINGOBJDATA
, "The object cannot be written that because it's missing internal data"},
26 {GIT_EPACKCORRUPTED
, "The packfile for the ODB is corrupted"},
27 {GIT_EFLOCKFAIL
, "Failed to adquire or release a file lock"},
28 {GIT_EZLIB
, "The Z library failed to inflate/deflate an object's data"},
29 {GIT_EBUSY
, "The queried object is currently busy"},
30 {GIT_EINVALIDPATH
, "The path is invalid"},
31 {GIT_EBAREINDEX
, "The index file is not backed up by an existing repository"},
32 {GIT_EINVALIDREFNAME
, "The name of the reference is not valid"},
33 {GIT_EREFCORRUPTED
, "The specified reference has its data corrupted"},
34 {GIT_ETOONESTEDSYMREF
, "The specified symbolic reference is too deeply nested"},
35 {GIT_EPACKEDREFSCORRUPTED
, "The pack-refs file is either corrupted of its format is not currently supported"},
36 {GIT_EINVALIDPATH
, "The path is invalid" },
37 {GIT_EREVWALKOVER
, "The revision walker is empty; there are no more commits left to iterate"},
38 {GIT_EINVALIDREFSTATE
, "The state of the reference is not valid"},
39 {GIT_ENOTIMPLEMENTED
, "This feature has not been implemented yet"},
40 {GIT_EEXISTS
, "A reference with this name already exists"},
41 {GIT_EOVERFLOW
, "The given integer literal is too large to be parsed"},
42 {GIT_ENOTNUM
, "The given literal is not a valid number"},
43 {GIT_EAMBIGUOUS
, "The given oid prefix is ambiguous"},
46 const char *git_strerror(int num
)
50 if (num
== GIT_EOSERR
)
51 return strerror(errno
);
52 for (i
= 0; i
< ARRAY_SIZE(error_codes
); i
++)
53 if (num
== error_codes
[i
].num
)
54 return error_codes
[i
].str
;
56 return "Unknown error";
59 #define ERROR_MAX_LEN 1024
61 void git___rethrow(const char *msg
, ...)
63 char new_error
[ERROR_MAX_LEN
];
65 char *old_error
= NULL
;
69 last_error
= GIT_GLOBAL
->error
.last
;
72 vsnprintf(new_error
, ERROR_MAX_LEN
, msg
, va
);
75 old_error
= git__strdup(last_error
);
77 snprintf(last_error
, ERROR_MAX_LEN
, "%s \n - %s", new_error
, old_error
);
82 void git___throw(const char *msg
, ...)
87 vsnprintf(GIT_GLOBAL
->error
.last
, ERROR_MAX_LEN
, msg
, va
);
91 const char *git_lasterror(void)
93 char *last_error
= GIT_GLOBAL
->error
.last
;
96 const git_error
*err
= git_error_last();
105 void git_clearerror(void)
107 char *last_error
= GIT_GLOBAL
->error
.last
;
108 last_error
[0] = '\0';
111 /********************************************
113 ********************************************/
115 static git_error g_git_oom_error
= {
120 void giterr_set_oom(void)
122 GIT_GLOBAL
->last_error
= &g_git_oom_error
;
125 void giterr_set(int error_class
, const char *string
, ...)
127 char error_str
[1024];
130 /* Grab errno before calling vsnprintf() so it won't be overwritten */
131 const char *os_error_msg
=
132 (error_class
== GITERR_OS
&& errno
!= 0) ? strerror(errno
) : NULL
;
134 DWORD dwLastError
= GetLastError();
137 va_start(arglist
, string
);
138 p_vsnprintf(error_str
, sizeof(error_str
), string
, arglist
);
141 /* automatically suffix strerror(errno) for GITERR_OS errors */
142 if (error_class
== GITERR_OS
) {
143 if (os_error_msg
!= NULL
) {
144 strncat(error_str
, ": ", sizeof(error_str
));
145 strncat(error_str
, os_error_msg
, sizeof(error_str
));
146 errno
= 0; /* reset so same error won't be reported twice */
149 else if (dwLastError
!= 0) {
150 LPVOID lpMsgBuf
= NULL
;
153 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
154 FORMAT_MESSAGE_FROM_SYSTEM
|
155 FORMAT_MESSAGE_IGNORE_INSERTS
,
156 NULL
, dwLastError
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
159 strncat(error_str
, ": ", sizeof(error_str
));
160 strncat(error_str
, (const char *)lpMsgBuf
, sizeof(error_str
));
169 giterr_set_str(error_class
, error_str
);
172 void giterr_set_str(int error_class
, const char *string
)
174 git_error
*error
= &GIT_GLOBAL
->error_t
;
176 free(error
->message
);
178 error
->message
= git__strdup(string
);
179 error
->klass
= error_class
;
181 if (error
->message
== NULL
) {
186 GIT_GLOBAL
->last_error
= error
;
189 void giterr_set_regex(const regex_t
*regex
, int error_code
)
191 char error_buf
[1024];
192 regerror(error_code
, regex
, error_buf
, sizeof(error_buf
));
193 giterr_set_str(GITERR_REGEX
, error_buf
);
196 void giterr_clear(void)
198 GIT_GLOBAL
->last_error
= NULL
;
201 const git_error
*git_error_last(void)
203 return GIT_GLOBAL
->last_error
;
206 void git_error_clear(void)