Welcome to little lamb

Code » limb » commit 5929880

Add ERR_* for all functions in limb

author Olivier Brunel
2023-02-17 09:12:22 UTC
committer Olivier Brunel
2023-02-20 09:01:33 UTC
parent 36cec3649eab301ac0e98bba03c77e6b69f95163

Add ERR_* for all functions in limb

Also macros to combine our error code with a (negavtive) return value,
from which both can later be extracted.

This can be useful to return a callback error both signaling which
callback failed as well as including its own return value (assuming it
doesn't use any of the 8 higher bits).

include/err.h +31 -0

diff --git a/include/err.h b/include/err.h
new file mode 100644
index 0000000..8f18fc2
--- /dev/null
+++ b/include/err.h
@@ -0,0 +1,31 @@
+#ifndef LIMB_ERR_H
+#define LIMB_ERR_H
+
+/* return values common to most functions in limb.*/
+enum {
+    ERR_OK          =  0, /* success */
+    ERR_MEM         = -1,
+    ERR_IO          = -2,
+    ERR_DATA        = -3, /* invalid data */
+    /* constructor-specific */
+    ERR_CB_PREINODE = -20,
+    ERR_CB_INODE    = -21,
+    ERR_CB_DATA     = -22,
+    ERR_CB_ROOT     = -23,
+    /* for general/uncategorized errors */
+    ERR_MISC        = -254,
+    /* something weird, should never happen. used for e.g. as init of ret */
+    ERR_UNKNOWN     = -255
+};
+
+
+/* merge a user return code r which *MUST BE* < 0 with an error code which *MUST
+ * BE* < 0 and > -256 into a single int (negative) value, from which both codes
+ * can then be extracted. */
+#define SET_ERR(err,r)  -(-r << 8 | -err)
+/* get the/our error code from the previous merging */
+#define GET_ERR(r)      -(-r & 0xFF)
+/* get the user code from the previous merging */
+#define GET_USER_ERR(r) -(-r >> 8)
+
+#endif /* LIMB_ERR_H */