Debian Codegraph @

plugins/vcs/GitException.h

raw
1 #pragma once
2
3 #include <stdexcept>
4 #include <git2.h>
5 #include "itextstream.h"
6
7 namespace vcs
8 {
9
10 namespace git
11 {
12
13 class GitException :
14 public std::runtime_error
15 {
16 public:
17 GitException(const std::string& message) :
18 runtime_error(GetLastErrorMessage() + "\n" + message)
19 {
20 rError() << "Git Exception: " << what() << std::endl;
21 }
22
23 GitException(int gitErrorCode) :
24 runtime_error(GetLastErrorMessage())
25 {
26 rError() << "Git Exception: " << what() << std::endl;
27 }
28
29 static std::string GetLastErrorMessage()
30 {
31 #if LIBGIT2_VER_MAJOR <= 0 && LIBGIT2_VER_MINOR < 28
32 // git_error_last has not been present before libgit2 0.28
33 auto error = giterr_last();
34 #else
35 auto error = git_error_last();
36 #endif
37
38 return error != nullptr ? error->message : "";
39 }
40
41 static void ThrowOnError(int errorCode)
42 {
43 if (errorCode == 0) return;
44
45 throw GitException(errorCode);
46 }
47 };
48
49 }
50
51 }