Skip to content

Commit f78f6bd

Browse files
committed
error functions: return an int
Stop returning a void for functions, future-proofing them to allow them to fail.
1 parent 4b331f0 commit f78f6bd

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

include/git2/errors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ GIT_EXTERN(void) git_error_clear(void);
143143
* @param error_class One of the `git_error_t` enum above describing the
144144
* general subsystem that is responsible for the error.
145145
* @param string The formatted error message to keep
146+
* @return 0 on success or -1 on failure
146147
*/
147-
GIT_EXTERN(void) git_error_set_str(int error_class, const char *string);
148+
GIT_EXTERN(int) git_error_set_str(int error_class, const char *string);
148149

149150
/**
150151
* Set the error message to a special value for memory allocation failure.

src/errors.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,25 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
9595
set_error_from_buffer(error_class);
9696
}
9797

98-
void git_error_set_str(int error_class, const char *string)
98+
int git_error_set_str(int error_class, const char *string)
9999
{
100100
git_buf *buf = &GIT_GLOBAL->error_buf;
101101

102102
assert(string);
103103

104-
if (!string)
105-
return;
104+
if (!string) {
105+
git_error_set(GIT_ERROR_INVALID, "unspecified caller error");
106+
return -1;
107+
}
106108

107109
git_buf_clear(buf);
108110
git_buf_puts(buf, string);
109-
if (!git_buf_oom(buf))
110-
set_error_from_buffer(error_class);
111+
112+
if (git_buf_oom(buf))
113+
return -1;
114+
115+
set_error_from_buffer(error_class);
116+
return 0;
111117
}
112118

113119
void git_error_clear(void)

0 commit comments

Comments
 (0)