feat(error): refactor error handling
This commit is contained in:
parent
34daac0361
commit
7f79d4c8c4
9 changed files with 23 additions and 23 deletions
|
|
@ -16,9 +16,11 @@ struct icmp_handle {
|
||||||
#define ICMP_ERR_RECV -4
|
#define ICMP_ERR_RECV -4
|
||||||
#define ICMP_ERR_INVALID -5
|
#define ICMP_ERR_INVALID -5
|
||||||
#define ICMP_ERR_ENOMEM -6
|
#define ICMP_ERR_ENOMEM -6
|
||||||
#define ICMP_ERR_AGAIN -7
|
#define ICMP_ERR_EAGAIN -7
|
||||||
|
|
||||||
/* Internal error handling functions */
|
/* Internal error handling functions */
|
||||||
void icmp_set_error(struct icmp_handle *h, int code, const char *msg);
|
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
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,4 @@ int socket_create(struct icmp_handle *h);
|
||||||
/* Configure socket (non-blocking, buffer size) */
|
/* Configure socket (non-blocking, buffer size) */
|
||||||
int socket_configure(struct icmp_handle *h);
|
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
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,12 @@ SRC_DIR = src
|
||||||
SRCS += $(SRC_DIR)/utils/checksum.c
|
SRCS += $(SRC_DIR)/utils/checksum.c
|
||||||
SRCS += $(SRC_DIR)/utils/time.c
|
SRCS += $(SRC_DIR)/utils/time.c
|
||||||
SRCS += $(SRC_DIR)/error/set_error.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/strerror.c
|
||||||
SRCS += $(SRC_DIR)/error/should_retry.c
|
SRCS += $(SRC_DIR)/error/should_retry.c
|
||||||
SRCS += $(SRC_DIR)/socket/create.c
|
SRCS += $(SRC_DIR)/socket/create.c
|
||||||
SRCS += $(SRC_DIR)/socket/configure.c
|
SRCS += $(SRC_DIR)/socket/configure.c
|
||||||
SRCS += $(SRC_DIR)/socket/utils.c
|
|
||||||
SRCS += $(SRC_DIR)/handle/create.c
|
SRCS += $(SRC_DIR)/handle/create.c
|
||||||
SRCS += $(SRC_DIR)/handle/destroy.c
|
SRCS += $(SRC_DIR)/handle/destroy.c
|
||||||
SRCS += $(SRC_DIR)/handle/get_fd.c
|
SRCS += $(SRC_DIR)/handle/get_fd.c
|
||||||
|
|
|
||||||
8
src/error/clear_error.c
Normal file
8
src/error/clear_error.c
Normal file
|
|
@ -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';
|
||||||
|
}
|
||||||
|
|
@ -3,14 +3,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void
|
void
|
||||||
socket_clear_error(struct icmp_handle *h)
|
icmp_set_error_fmt(struct icmp_handle *h, int code, const char *fmt, ...)
|
||||||
{
|
|
||||||
h->last_error = ICMP_OK;
|
|
||||||
h->error_msg[0] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
socket_set_error(struct icmp_handle *h, int code, const char *fmt, ...)
|
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
|
|
@ -9,5 +9,5 @@ icmp_should_retry(const icmp_handle_t *h)
|
||||||
if (NULL == handle)
|
if (NULL == handle)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return (ICMP_ERR_AGAIN == handle->last_error) ? 1 : 0;
|
return (ICMP_ERR_EAGAIN == handle->last_error) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ socket_configure(struct icmp_handle *h)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clear error state on success */
|
/* Clear error state on success */
|
||||||
socket_clear_error(h);
|
icmp_clear_error(h);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -37,6 +37,6 @@ socket_configure(struct icmp_handle *h)
|
||||||
static void
|
static void
|
||||||
handle_configure_error(struct icmp_handle *h, const char *func)
|
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));
|
"%s failed: %s", func, strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ socket_create(struct icmp_handle *h)
|
||||||
h->fd = fd;
|
h->fd = fd;
|
||||||
|
|
||||||
/* Clear error state on success */
|
/* Clear error state on success */
|
||||||
socket_clear_error(h);
|
icmp_clear_error(h);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -37,12 +37,12 @@ handle_socket_error(struct icmp_handle *h, int err)
|
||||||
{
|
{
|
||||||
if (EPERM == 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");
|
"Raw socket requires CAP_NET_RAW or root");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
socket_set_error(h, ICMP_ERR_SOCKET,
|
icmp_set_error_fmt(h, ICMP_ERR_SOCKET,
|
||||||
"socket() failed: %s", strerror(err));
|
"socket() failed: %s", strerror(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,16 +57,16 @@ Test(error, strerror_no_error)
|
||||||
cr_assert_str_eq(msg, "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)
|
Test(error, should_retry_with_eagain)
|
||||||
{
|
{
|
||||||
struct icmp_handle h = {0};
|
struct icmp_handle h = {0};
|
||||||
h.last_error = ICMP_ERR_AGAIN;
|
h.last_error = ICMP_ERR_EAGAIN;
|
||||||
strcpy(h.error_msg, "Send would block");
|
strcpy(h.error_msg, "Send would block");
|
||||||
|
|
||||||
int should_retry = icmp_should_retry((icmp_handle_t *)&h);
|
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 */
|
/* Test 8: should_retry returns 0 for other errors */
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue