]>
Commit | Line | Data |
---|---|---|
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 | ||
ae234862 AE |
12 | static struct { |
13 | int num; | |
14 | const char *str; | |
15 | } error_codes[] = { | |
a8bfce69 VM |
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"}, | |
05314b5b | 24 | {GIT_EINVALIDTYPE, "The object or config variable type is invalid or doesn't match"}, |
a8bfce69 VM |
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"}, | |
f2c24713 | 30 | {GIT_EINVALIDPATH, "The path is invalid"}, |
9282e921 | 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"}, | |
c836c332 VM |
35 | {GIT_EPACKEDREFSCORRUPTED, "The pack-refs file is either corrupted of its format is not currently supported"}, |
36 | {GIT_EINVALIDPATH, "The path is invalid" }, | |
6a0895ad | 37 | {GIT_EREVWALKOVER, "The revision walker is empty; there are no more commits left to iterate"}, |
72a3fe42 | 38 | {GIT_EINVALIDREFSTATE, "The state of the reference is not valid"}, |
baad182c | 39 | {GIT_ENOTIMPLEMENTED, "This feature has not been implemented yet"}, |
c6e65aca VM |
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"}, | |
53c0bd81 | 43 | {GIT_EAMBIGUOUSOIDPREFIX, "The given oid prefix is ambiguous"}, |
ae234862 AE |
44 | }; |
45 | ||
46 | const char *git_strerror(int num) | |
47 | { | |
0ef9d2aa | 48 | size_t i; |
7dd8a9f7 SP |
49 | |
50 | if (num == GIT_EOSERR) | |
51 | return strerror(errno); | |
ae234862 AE |
52 | for (i = 0; i < ARRAY_SIZE(error_codes); i++) |
53 | if (num == error_codes[i].num) | |
54 | return error_codes[i].str; | |
55 | ||
56 | return "Unknown error"; | |
57 | } | |
f4a936b5 | 58 | |
a15c550d VM |
59 | #define ERROR_MAX_LEN 1024 |
60 | ||
d7f0abab | 61 | void git___rethrow(const char *msg, ...) |
fa59f18d | 62 | { |
a15c550d VM |
63 | char new_error[ERROR_MAX_LEN]; |
64 | char *last_error; | |
fa59f18d VM |
65 | char *old_error = NULL; |
66 | ||
67 | va_list va; | |
68 | ||
a15c550d VM |
69 | last_error = GIT_GLOBAL->error.last; |
70 | ||
fa59f18d | 71 | va_start(va, msg); |
a15c550d | 72 | vsnprintf(new_error, ERROR_MAX_LEN, msg, va); |
fa59f18d VM |
73 | va_end(va); |
74 | ||
a15c550d VM |
75 | old_error = git__strdup(last_error); |
76 | ||
77 | snprintf(last_error, ERROR_MAX_LEN, "%s \n - %s", new_error, old_error); | |
78 | ||
3286c408 | 79 | git__free(old_error); |
fa59f18d VM |
80 | } |
81 | ||
d7f0abab | 82 | void git___throw(const char *msg, ...) |
02f9e637 VM |
83 | { |
84 | va_list va; | |
85 | ||
86 | va_start(va, msg); | |
a15c550d | 87 | vsnprintf(GIT_GLOBAL->error.last, ERROR_MAX_LEN, msg, va); |
02f9e637 | 88 | va_end(va); |
02f9e637 | 89 | } |
5eb0fab8 VM |
90 | |
91 | const char *git_lasterror(void) | |
92 | { | |
a15c550d VM |
93 | char *last_error = GIT_GLOBAL->error.last; |
94 | ||
95 | if (!last_error[0]) | |
ab7941b5 VM |
96 | return NULL; |
97 | ||
a15c550d | 98 | return last_error; |
5eb0fab8 VM |
99 | } |
100 | ||
ab7941b5 VM |
101 | void git_clearerror(void) |
102 | { | |
a15c550d VM |
103 | char *last_error = GIT_GLOBAL->error.last; |
104 | last_error[0] = '\0'; | |
ab7941b5 | 105 | } |
60bc2d20 VM |
106 | |
107 | /******************************************** | |
108 | * New error handling | |
109 | ********************************************/ | |
110 | ||
1a481123 VM |
111 | static git_error g_git_oom_error = { |
112 | "Out of memory", | |
113 | GITERR_NOMEMORY | |
114 | }; | |
115 | ||
116 | void giterr_set_oom(void) | |
117 | { | |
118 | GIT_GLOBAL->last_error = &g_git_oom_error; | |
119 | } | |
120 | ||
121 | void giterr_set(int error_class, const char *string, ...) | |
60bc2d20 VM |
122 | { |
123 | char error_str[1024]; | |
124 | va_list arglist; | |
60bc2d20 VM |
125 | |
126 | va_start(arglist, string); | |
127 | p_vsnprintf(error_str, sizeof(error_str), string, arglist); | |
128 | va_end(arglist); | |
129 | ||
ae9e29fd | 130 | /* automatically suffix strerror(errno) for GITERR_OS errors */ |
dda708e7 | 131 | if (error_class == GITERR_OS) { |
ae9e29fd | 132 | strncat(error_str, ": ", sizeof(error_str)); |
dda708e7 | 133 | strncat(error_str, strerror(errno), sizeof(error_str)); |
ae9e29fd RB |
134 | errno = 0; |
135 | } | |
136 | ||
dda708e7 VM |
137 | giterr_set_str(error_class, error_str); |
138 | } | |
139 | ||
140 | void giterr_set_str(int error_class, const char *string) | |
141 | { | |
142 | git_error *error = &GIT_GLOBAL->error_t; | |
143 | ||
144 | free(error->message); | |
145 | ||
146 | error->message = git__strdup(string); | |
60bc2d20 VM |
147 | error->klass = error_class; |
148 | ||
149 | if (error->message == NULL) { | |
1a481123 | 150 | giterr_set_oom(); |
60bc2d20 VM |
151 | return; |
152 | } | |
153 | ||
1a481123 | 154 | GIT_GLOBAL->last_error = error; |
60bc2d20 VM |
155 | } |
156 | ||
dda708e7 VM |
157 | void giterr_set_regex(const regex_t *regex, int error_code) |
158 | { | |
159 | char error_buf[1024]; | |
160 | regerror(error_code, regex, error_buf, sizeof(error_buf)); | |
161 | giterr_set_str(GITERR_REGEX, error_buf); | |
162 | } | |
163 | ||
1a481123 | 164 | void giterr_clear(void) |
60bc2d20 | 165 | { |
1a481123 | 166 | GIT_GLOBAL->last_error = NULL; |
60bc2d20 | 167 | } |