From 7f79d4c8c45f85227f0bc97563f8d68a8cebf0e3 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Sun, 25 Jan 2026 15:10:18 +0100 Subject: [PATCH] feat(error): refactor error handling --- includes/internal/icmp_internal.h | 4 +++- includes/internal/icmp_socket.h | 4 ---- sources.mk | 3 ++- src/error/clear_error.c | 8 ++++++++ src/{socket/utils.c => error/set_error_fmt.c} | 9 +-------- src/error/should_retry.c | 2 +- src/socket/configure.c | 4 ++-- src/socket/create.c | 6 +++--- tests/error/test_error.c | 6 +++--- 9 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 src/error/clear_error.c rename src/{socket/utils.c => error/set_error_fmt.c} (55%) diff --git a/includes/internal/icmp_internal.h b/includes/internal/icmp_internal.h index cedd274..96e73db 100644 --- a/includes/internal/icmp_internal.h +++ b/includes/internal/icmp_internal.h @@ -16,9 +16,11 @@ struct icmp_handle { #define ICMP_ERR_RECV -4 #define ICMP_ERR_INVALID -5 #define ICMP_ERR_ENOMEM -6 -#define ICMP_ERR_AGAIN -7 +#define ICMP_ERR_EAGAIN -7 /* Internal error handling functions */ void icmp_set_error(struct icmp_handle *h, int code, const char *msg); +void icmp_set_error_fmt(struct icmp_handle *h, int code, const char *fmt, ...); +void icmp_clear_error(struct icmp_handle *h); #endif diff --git a/includes/internal/icmp_socket.h b/includes/internal/icmp_socket.h index fb8889b..9a42daf 100644 --- a/includes/internal/icmp_socket.h +++ b/includes/internal/icmp_socket.h @@ -9,8 +9,4 @@ int socket_create(struct icmp_handle *h); /* Configure socket (non-blocking, buffer size) */ int socket_configure(struct icmp_handle *h); -/* Internal helpers for error handling */ -void socket_clear_error(struct icmp_handle *h); -void socket_set_error(struct icmp_handle *h, int code, const char *fmt, ...); - #endif diff --git a/sources.mk b/sources.mk index 49e35cb..e3cbefa 100644 --- a/sources.mk +++ b/sources.mk @@ -2,11 +2,12 @@ SRC_DIR = src SRCS += $(SRC_DIR)/utils/checksum.c SRCS += $(SRC_DIR)/utils/time.c SRCS += $(SRC_DIR)/error/set_error.c +SRCS += $(SRC_DIR)/error/set_error_fmt.c +SRCS += $(SRC_DIR)/error/clear_error.c SRCS += $(SRC_DIR)/error/strerror.c SRCS += $(SRC_DIR)/error/should_retry.c SRCS += $(SRC_DIR)/socket/create.c SRCS += $(SRC_DIR)/socket/configure.c -SRCS += $(SRC_DIR)/socket/utils.c SRCS += $(SRC_DIR)/handle/create.c SRCS += $(SRC_DIR)/handle/destroy.c SRCS += $(SRC_DIR)/handle/get_fd.c diff --git a/src/error/clear_error.c b/src/error/clear_error.c new file mode 100644 index 0000000..7b16721 --- /dev/null +++ b/src/error/clear_error.c @@ -0,0 +1,8 @@ +#include "internal/icmp_internal.h" + +void +icmp_clear_error(struct icmp_handle *h) +{ + h->last_error = ICMP_OK; + h->error_msg[0] = '\0'; +} diff --git a/src/socket/utils.c b/src/error/set_error_fmt.c similarity index 55% rename from src/socket/utils.c rename to src/error/set_error_fmt.c index cbedd17..edd546e 100644 --- a/src/socket/utils.c +++ b/src/error/set_error_fmt.c @@ -3,14 +3,7 @@ #include void -socket_clear_error(struct icmp_handle *h) -{ - h->last_error = ICMP_OK; - h->error_msg[0] = '\0'; -} - -void -socket_set_error(struct icmp_handle *h, int code, const char *fmt, ...) +icmp_set_error_fmt(struct icmp_handle *h, int code, const char *fmt, ...) { va_list args; diff --git a/src/error/should_retry.c b/src/error/should_retry.c index cc5437d..7420dfe 100644 --- a/src/error/should_retry.c +++ b/src/error/should_retry.c @@ -9,5 +9,5 @@ icmp_should_retry(const icmp_handle_t *h) if (NULL == handle) return 0; - return (ICMP_ERR_AGAIN == handle->last_error) ? 1 : 0; + return (ICMP_ERR_EAGAIN == handle->last_error) ? 1 : 0; } diff --git a/src/socket/configure.c b/src/socket/configure.c index 094da39..d534a65 100644 --- a/src/socket/configure.c +++ b/src/socket/configure.c @@ -29,7 +29,7 @@ socket_configure(struct icmp_handle *h) } /* Clear error state on success */ - socket_clear_error(h); + icmp_clear_error(h); return 0; } @@ -37,6 +37,6 @@ socket_configure(struct icmp_handle *h) static void handle_configure_error(struct icmp_handle *h, const char *func) { - socket_set_error(h, ICMP_ERR_SOCKET, + icmp_set_error_fmt(h, ICMP_ERR_SOCKET, "%s failed: %s", func, strerror(errno)); } diff --git a/src/socket/create.c b/src/socket/create.c index 566f387..12a9f5f 100644 --- a/src/socket/create.c +++ b/src/socket/create.c @@ -27,7 +27,7 @@ socket_create(struct icmp_handle *h) h->fd = fd; /* Clear error state on success */ - socket_clear_error(h); + icmp_clear_error(h); return 0; } @@ -37,12 +37,12 @@ handle_socket_error(struct icmp_handle *h, int err) { if (EPERM == err) { - socket_set_error(h, ICMP_ERR_PERMISSION, + icmp_set_error_fmt(h, ICMP_ERR_PERMISSION, "Raw socket requires CAP_NET_RAW or root"); } else { - socket_set_error(h, ICMP_ERR_SOCKET, + icmp_set_error_fmt(h, ICMP_ERR_SOCKET, "socket() failed: %s", strerror(err)); } } diff --git a/tests/error/test_error.c b/tests/error/test_error.c index ecc821c..14549d0 100644 --- a/tests/error/test_error.c +++ b/tests/error/test_error.c @@ -57,16 +57,16 @@ Test(error, strerror_no_error) cr_assert_str_eq(msg, "No error"); } -/* Test 7: should_retry returns 1 for ICMP_ERR_AGAIN */ +/* Test 7: should_retry returns 1 for ICMP_ERR_EAGAIN */ Test(error, should_retry_with_eagain) { struct icmp_handle h = {0}; - h.last_error = ICMP_ERR_AGAIN; + h.last_error = ICMP_ERR_EAGAIN; strcpy(h.error_msg, "Send would block"); int should_retry = icmp_should_retry((icmp_handle_t *)&h); - cr_assert_eq(should_retry, 1, "Should return 1 for ICMP_ERR_AGAIN"); + cr_assert_eq(should_retry, 1, "Should return 1 for ICMP_ERR_EAGAIN"); } /* Test 8: should_retry returns 0 for other errors */