Update StatusTuple to use enum Code
@@ -23,6 +23,22 @@ namespace ebpf {
class StatusTuple {
public:
+ enum class Code {
+ // Not an error, indicates success.
+ OK = 0,
+ // For any error that is not covered in the existing codes.
+ UNKNOWN,
+
+ INVALID_ARGUMENT,
+ PERMISSION_DENIED,
+ // For any error that was raised when making syscalls.
+ SYSTEM,
+ };
+
+ static StatusTuple OK() {
+ return StatusTuple(Code::OK, "");
+ }
+
StatusTuple(int ret) : ret_(ret) {}
StatusTuple(int ret, const char *msg) : ret_(ret), msg_(msg) {}
@@ -36,16 +52,34 @@ public:
msg_ = std::string(buf);
}
+ StatusTuple(Code code, const std::string &msg) : use_enum_code_(true), code_(code), msg_(msg) {}
+
void append_msg(const std::string& msg) {
msg_ += msg;
}
- int code() const { return ret_; }
+ bool ok() const {
+ if (use_enum_code_) {
+ return code_ == Code::OK;
+ }
+ return ret_ == 0;
+ }
+
+ int code() const {
+ if (use_enum_code_) {
+ return static_cast<int>(code_);
+ }
+ return ret_;
+ }
const std::string& msg() const { return msg_; }
private:
int ret_;
+
+ bool use_enum_code_ = false;
+ Code code_;
+
std::string msg_;
};
@@ -57,4 +91,21 @@ private:
} \
} while (0)
+namespace error {
+
+#define DECLARE_ERROR(FN, CODE) \
+ inline StatusTuple FN(const std::string& msg) { \
+ return StatusTuple(::ebpf::StatusTuple::Code::CODE, msg); \
+ } \
+ inline bool Is##FN(const StatusTuple& status) { \
+ return status.code() == static_cast<int>(::ebpf::StatusTuple::Code::CODE); \
+ }
+
+DECLARE_ERROR(Unknown, UNKNOWN)
+DECLARE_ERROR(InvalidArgument, INVALID_ARGUMENT)
+DECLARE_ERROR(PermissionDenied, PERMISSION_DENIED)
+DECLARE_ERROR(System, SYSTEM)
+
+} // namespace error
+
} // namespace ebpf
