"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
add got_error_fmt()
To:
gameoftrees@openbsd.org
Date:
Wed, 16 Dec 2020 00:16:34 +0100

Download raw body.

Thread
  • Stefan Sperling:

    add got_error_fmt()

This patch adds a got_error_fmt() function.

got_error_fmt() could eventually replace got_error_path() which has already
been used to construct errors with strings that are not actually paths...

ok?

diff 2ec5b3604b0907fe87969d9929f01f4339627a19 c097ea237ccace7de5829fb7d1a38cb421aacace
blob - d3127f6f9acf465b17b825b7deb3d42b7a42fe7d
blob + 9244c2c00f8a56e7ef54417632a95c0f7aa36ddd
--- include/got_error.h
+++ include/got_error.h
@@ -380,3 +380,10 @@ const struct got_error *got_error_uuid(uint32_t, const
 
 /* Return an error with a path prefixed to the error message. */
 const struct got_error *got_error_path(const char *, int);
+
+/*
+ * Return an error with an error message prefix built by vsnprintf(3)
+ * from the provided format string and the variable-length list of
+ * additional arguments.
+*/
+const struct got_error *got_error_fmt(int, const char *, ...);
blob - f8cf59b20a799b2b360c647f8eb7f1c331257431
blob + 772c50ed2ea44fc86f4cb411b60f6bbf55ef0a88
--- lib/error.c
+++ lib/error.c
@@ -212,3 +212,29 @@ got_error_path(const char *path, int code)
 
 	abort();
 }
+
+const struct got_error *
+got_error_fmt(int code, const char *fmt, ...)
+{
+	static struct got_error err;
+	static char msg[PATH_MAX * 4 + 128];
+	char buf[PATH_MAX * 4];
+	va_list ap;
+	size_t i;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	va_end(ap);
+
+	for (i = 0; i < nitems(got_errors); i++) {
+		if (code == got_errors[i].code) {
+			err.code = code;
+			snprintf(msg, sizeof(msg), "%s: %s", buf,
+			    got_errors[i].msg);
+			err.msg = msg;
+			return &err;
+		}
+	}
+
+	abort();
+}