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