Skip to content

Commit be2f309

Browse files
committed
edit error codes
1 parent 666b5bd commit be2f309

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/subcommand/diff_subcommand.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <termcolor/termcolor.hpp>
66

77
#include "../utils/common.hpp"
8+
#include "../utils/git_exception.hpp"
89
#include "../subcommand/diff_subcommand.hpp"
910
#include "../wrapper/patch_wrapper.hpp"
1011
#include "../wrapper/repository_wrapper.hpp"
@@ -212,7 +213,7 @@ diff_wrapper compute_diff_no_index(std::vector<std::string> files, git_diff_opti
212213
{
213214
if (files.size() != 2)
214215
{
215-
throw git_exception("two files should be provided as arguments", -1); //TODO: check error + code
216+
throw git_exception("usage: git diff --no-index [<options>] <path> <path> [<pathspec>...]", git2cpp_error_code::BAD_ARGUMENT);
216217
}
217218

218219
git_diff_options_init(&diffopts, GIT_DIFF_OPTIONS_VERSION);
@@ -222,11 +223,11 @@ diff_wrapper compute_diff_no_index(std::vector<std::string> files, git_diff_opti
222223

223224
if (file1_str.empty())
224225
{
225-
throw git_exception("file " + files[0] + " cannot be read", -1); //TODO: check error + code
226+
throw git_exception("Cannot read file: " + files[0], git2cpp_error_code::GENERIC_ERROR); //TODO: check error code with git
226227
}
227228
if (file2_str.empty())
228229
{
229-
throw git_exception("file " + files[1] + " cannot be read", -1); //TODO: check error + code
230+
throw git_exception("Cannot read file: " + files[1], git2cpp_error_code::GENERIC_ERROR); //TODO: check error code with git
230231
}
231232

232233
auto patch = patch_wrapper::patch_from_files(files[0], file1_str, files[1], file2_str, &diffopts);

src/utils/common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ std::string read_file(const std::string& path)
109109
std::ifstream file(path, std::ios::binary);
110110
if (!file)
111111
{
112-
throw git_exception("Cannot read file: " + path, -1);
112+
throw git_exception("error: Could not access " + path, git2cpp_error_code::GENERIC_ERROR);
113113
}
114114
std::stringstream buffer;
115115
buffer << file.rdbuf();

src/utils/git_exception.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ void throw_if_error(int exit_code)
1010
}
1111
}
1212

13-
1413
git_exception::git_exception(const std::string_view message, int error_code)
1514
: m_message(message), m_error_code(error_code)
1615
{}
1716

17+
git_exception::git_exception(const std::string_view message, git2cpp_error_code error_code)
18+
: git_exception(message, static_cast<int>(error_code))
19+
{}
20+
1821
int git_exception::error_code() const
1922
{
2023
return m_error_code;

src/utils/git_exception.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
#include <exception>
44
#include <string>
55

6+
enum class git2cpp_error_code
7+
{
8+
GENERIC_ERROR = -1,
9+
BAD_ARGUMENT = 129,
10+
};
11+
612
void throw_if_error(int exit_code);
713

814
class git_exception : public std::exception
915
{
1016
public:
1117

1218
git_exception(const std::string_view message, int error_code);
19+
git_exception(const std::string_view message, git2cpp_error_code error_code);
1320

1421
int error_code() const;
1522

0 commit comments

Comments
 (0)