]> git.proxmox.com Git - libgit2.git/blob - src/errors.c
Merge pull request #398 from carlosmn/config-autohome
[libgit2.git] / src / errors.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
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.
6 */
7 #include "common.h"
8 #include "git2/thread-utils.h" /* for GIT_TLS */
9 #include "thread-utils.h" /* for GIT_TLS */
10
11 #include <stdarg.h>
12
13 static GIT_TLS char g_last_error[1024];
14
15 static struct {
16 int num;
17 const char *str;
18 } error_codes[] = {
19 {GIT_ERROR, "Unspecified error"},
20 {GIT_ENOTOID, "Input was not a properly formatted Git object id."},
21 {GIT_ENOTFOUND, "Object does not exist in the scope searched."},
22 {GIT_ENOMEM, "Not enough space available."},
23 {GIT_EOSERR, "Consult the OS error information."},
24 {GIT_EOBJTYPE, "The specified object is of invalid type"},
25 {GIT_EOBJCORRUPTED, "The specified object has its data corrupted"},
26 {GIT_ENOTAREPO, "The specified repository is invalid"},
27 {GIT_EINVALIDTYPE, "The object or config variable type is invalid or doesn't match"},
28 {GIT_EMISSINGOBJDATA, "The object cannot be written that because it's missing internal data"},
29 {GIT_EPACKCORRUPTED, "The packfile for the ODB is corrupted"},
30 {GIT_EFLOCKFAIL, "Failed to adquire or release a file lock"},
31 {GIT_EZLIB, "The Z library failed to inflate/deflate an object's data"},
32 {GIT_EBUSY, "The queried object is currently busy"},
33 {GIT_EINVALIDPATH, "The path is invalid"},
34 {GIT_EBAREINDEX, "The index file is not backed up by an existing repository"},
35 {GIT_EINVALIDREFNAME, "The name of the reference is not valid"},
36 {GIT_EREFCORRUPTED, "The specified reference has its data corrupted"},
37 {GIT_ETOONESTEDSYMREF, "The specified symbolic reference is too deeply nested"},
38 {GIT_EPACKEDREFSCORRUPTED, "The pack-refs file is either corrupted of its format is not currently supported"},
39 {GIT_EINVALIDPATH, "The path is invalid" },
40 {GIT_EREVWALKOVER, "The revision walker is empty; there are no more commits left to iterate"},
41 {GIT_EINVALIDREFSTATE, "The state of the reference is not valid"},
42 {GIT_ENOTIMPLEMENTED, "This feature has not been implemented yet"},
43 {GIT_EEXISTS, "A reference with this name already exists"},
44 {GIT_EOVERFLOW, "The given integer literal is too large to be parsed"},
45 {GIT_ENOTNUM, "The given literal is not a valid number"},
46 {GIT_EAMBIGUOUSOIDPREFIX, "The given oid prefix is ambiguous"},
47 };
48
49 const char *git_strerror(int num)
50 {
51 size_t i;
52
53 if (num == GIT_EOSERR)
54 return strerror(errno);
55 for (i = 0; i < ARRAY_SIZE(error_codes); i++)
56 if (num == error_codes[i].num)
57 return error_codes[i].str;
58
59 return "Unknown error";
60 }
61
62 void git___rethrow(const char *msg, ...)
63 {
64 char new_error[1024];
65 char *old_error = NULL;
66
67 va_list va;
68
69 va_start(va, msg);
70 vsnprintf(new_error, sizeof(new_error), msg, va);
71 va_end(va);
72
73 old_error = strdup(g_last_error);
74 snprintf(g_last_error, sizeof(g_last_error), "%s \n - %s", new_error, old_error);
75 free(old_error);
76 }
77
78 void git___throw(const char *msg, ...)
79 {
80 va_list va;
81
82 va_start(va, msg);
83 vsnprintf(g_last_error, sizeof(g_last_error), msg, va);
84 va_end(va);
85 }
86
87 const char *git_lasterror(void)
88 {
89 if (!g_last_error[0])
90 return NULL;
91
92 return g_last_error;
93 }
94
95 void git_clearerror(void)
96 {
97 g_last_error[0] = '\0';
98 }