1# Classed error handling
2# Todo: kclass is ignored right now, would this be useful to report?
3raise_libgit2_error <- function(code, message, where = "", kclass = 0){
4  cl <- call("::", substitute(libgit2), as.name(where))
5  e <- structure(
6    class = c(libgit2_error_name(code), "libgit2_error", "error", "condition"),
7    list(message = message, call = cl) #call must be an R expression
8  )
9  stop(e)
10}
11
12libgit2_error_name <- function(x){
13  out <- which(libgit2_error_codes == x)
14  if(length(out))
15    return(names(out))
16  return("UNKNOWN_ERROR_CODE")
17}
18
19# Eror codes copied from https://github.com/libgit2/libgit2/blame/master/include/git2/errors.h
20# We don't do this in C because the list of error codes changes from version to version
21libgit2_error_codes <- c(
22  GIT_OK         =  0,
23  GIT_ERROR      = -1,
24  GIT_ENOTFOUND  = -3,
25  GIT_EEXISTS    = -4,
26  GIT_EAMBIGUOUS = -5,
27  GIT_EBUFS      = -6,
28  GIT_EUSER      = -7,
29  GIT_EBAREREPO       =  -8,
30  GIT_EUNBORNBRANCH   =  -9,
31  GIT_EUNMERGED       = -10,
32  GIT_ENONFASTFORWARD = -11,
33  GIT_EINVALIDSPEC    = -12,
34  GIT_ECONFLICT       = -13,
35  GIT_ELOCKED         = -14,
36  GIT_EMODIFIED       = -15,
37  GIT_EAUTH           = -16,
38  GIT_ECERTIFICATE    = -17,
39  GIT_EAPPLIED        = -18,
40  GIT_EPEEL           = -19,
41  GIT_EEOF            = -20,
42  GIT_EINVALID        = -21,
43  GIT_EUNCOMMITTED    = -22,
44  GIT_EDIRECTORY      = -23,
45  GIT_EMERGECONFLICT  = -24,
46  GIT_PASSTHROUGH     = -30,
47  GIT_ITEROVER        = -31,
48  GIT_RETRY           = -32,
49  GIT_EMISMATCH       = -33,
50  GIT_EINDEXDIRTY     = -34,
51  GIT_EAPPLYFAIL      = -35
52)
53