Skip to content

Commit

Permalink
Isolate error codes definition from iproto.[ch] to errcode.[ch].
Browse files Browse the repository at this point in the history
Add errcode_record structure that contains string represenataion of
an error, numerical value of an error to send to a client, and
textual description of an error.
  • Loading branch information
rtokarev committed May 11, 2011
1 parent 3e2de4b commit 72bfb7f
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 69 deletions.
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Expand Up @@ -56,7 +56,7 @@ set (recompiled_core_sources

set (common_sources tbuf.c palloc.c util.c diagnostics.c
salloc.c pickle.c coro.c stat.c log_io.c
log_io_remote.c iproto.c)
log_io_remote.c iproto.c errcode.c)

if (ENABLE_TRACE)
set (common_sources ${common_sources} trace.c)
Expand Down
3 changes: 3 additions & 0 deletions core/errcode.c
@@ -0,0 +1,3 @@
#include <errcode.h>

ERRCODE_RECORDS(error_codes, ERROR_CODES);
6 changes: 3 additions & 3 deletions core/iproto.c
Expand Up @@ -27,15 +27,14 @@
#include <stdio.h>
#include <string.h>

#include <errcode.h>
#include <palloc.h>
#include <fiber.h>
#include <iproto.h>
#include <tbuf.h>
#include <say.h>

const uint32_t msg_ping = 0xff00;
STRS(error_codes, ERROR_CODES);
DESC_STRS(error_codes, ERROR_CODES);

static struct tbuf *
iproto_parse(struct tbuf *in)
Expand Down Expand Up @@ -76,7 +75,8 @@ iproto_interact(void *data)
u32 msg_code = iproto(request)->msg_code;
request->len = iproto(request)->len;
request->data = iproto(request)->data;
reply->ret_code = callback(msg_code, request);
u32 err = callback(msg_code, request);
reply->ret_code = ERRCODE_VAL(error_codes, err);

/*
* retcode is uint32_t and included int struct iproto_header_retcode
Expand Down
2 changes: 1 addition & 1 deletion core/pickle.c
Expand Up @@ -30,7 +30,7 @@
#include <tbuf.h>
#include <palloc.h>
#include <fiber.h>
#include <iproto.h> /* for err codes */
#include <errcode.h>
#include <pickle.h>

/* caller must ensure that there is space in target */
Expand Down
65 changes: 65 additions & 0 deletions include/errcode.h
@@ -0,0 +1,65 @@
#ifndef TARANTOOL_ERRCODE_H
#define TARANTOOL_ERRCODE_H

#include <stdint.h>

#include <util.h>

struct errcode_record {
const char *errstr;
uint32_t errval;
const char *errdesc;
};

#define ERRCODE_RECORD_MEMBER(s, v, d) { \
.errstr = #s, \
.errval = v, \
.errdesc = #d \
},

#define ERRCODE_RECORDS(enum_name, enum_members) \
struct errcode_record enum_name##_records[enum_name##_MAX] = { \
enum_members(ERRCODE_RECORD_MEMBER) \
}

#define ERRCODE_STR(enum_name, err) (enum_name##_records[err].errstr)
#define ERRCODE_VAL(enum_name, err) (enum_name##_records[err].errval)
#define ERRCODE_DESC(enum_name, err) (enum_name##_records[err].errdesc)

#define ERROR_CODES(_) \
_(ERR_CODE_OK, 0x00000000, "OK") \
_(ERR_CODE_NONMASTER, 0x00000102, "Non master connection, but it should be") \
_(ERR_CODE_ILLEGAL_PARAMS, 0x00000202, "Illegal parametrs") \
_(ERR_CODE_BAD_UID, 0x00000302, "Uid is not from this storage range") \
_(ERR_CODE_NODE_IS_RO, 0x00000401, "Node is marked as read-only") \
_(ERR_CODE_NODE_IS_NOT_LOCKED, 0x00000501, "Node isn't locked") \
_(ERR_CODE_NODE_IS_LOCKED, 0x00000601, "Node is locked") \
_(ERR_CODE_MEMORY_ISSUE, 0x00000701, "Some memory issue") \
_(ERR_CODE_BAD_INTEGRITY, 0x00000802, "Bad graph integrity") \
_(ERR_CODE_UNSUPPORTED_COMMAND, 0x00000a02, "Unsupported command") \
/* gap due to silverproxy */ \
_(ERR_CODE_CANNOT_REGISTER, 0x00001801, "Can't register new user") \
_(ERR_CODE_CANNOT_INIT_ALERT_ID, 0x00001a01, "Can't generate alert id") \
_(ERR_CODE_CANNOT_DEL, 0x00001b02, "Can't del node") \
_(ERR_CODE_USER_NOT_REGISTERED, 0x00001c02, "User isn't registered") \
/* silversearch error codes */ \
_(ERR_CODE_SYNTAX_ERROR, 0x00001d02, "Syntax error in query") \
_(ERR_CODE_WRONG_FIELD, 0x00001e02, "Unknown field") \
_(ERR_CODE_WRONG_NUMBER, 0x00001f02, "Number value is out of range") \
_(ERR_CODE_DUPLICATE, 0x00002002, "Insert already existing object") \
_(ERR_CODE_UNSUPPORTED_ORDER, 0x00002202, "Can not order result") \
_(ERR_CODE_MULTIWRITE, 0x00002302, "Multiple to update/delete") \
_(ERR_CODE_NOTHING, 0x00002400, "Nothing to do (not an error)") \
_(ERR_CODE_UPDATE_ID, 0x00002502, "Id's update") \
_(ERR_CODE_WRONG_VERSION, 0x00002602, "Unsupported version of protocol") \
/* other generic error codes */ \
_(ERR_CODE_UNKNOWN_ERROR, 0x00002702, "Unknown error") \
_(ERR_CODE_NODE_NOT_FOUND, 0x00003102, "Node isn't found") \
_(ERR_CODE_NODE_FOUND, 0x00003702, "Node is found") \
_(ERR_CODE_INDEX_VIOLATION, 0x00003802, "Some index violation occur") \
_(ERR_CODE_NO_SUCH_NAMESPACE, 0x00003902, "There is no such namespace")

ENUM0(error_codes, ERROR_CODES);
extern struct errcode_record error_codes_records[];

#endif
6 changes: 3 additions & 3 deletions include/fiber.h
Expand Up @@ -127,12 +127,12 @@ void raise_(int);
struct msg *read_inbox(void);
int fiber_bread(struct tbuf *, size_t v);

inline static void add_iov_unsafe(void *buf, size_t len)
inline static void add_iov_unsafe(const void *buf, size_t len)
{
struct iovec *v;
assert(fiber->iov->size - fiber->iov->len >= sizeof(*v));
v = fiber->iov->data + fiber->iov->len;
v->iov_base = buf;
v->iov_base = (void *)buf;
v->iov_len = len;
fiber->iov->len += sizeof(*v);
fiber->iov_cnt++;
Expand All @@ -143,7 +143,7 @@ inline static void iov_ensure(size_t count)
tbuf_ensure(fiber->iov, sizeof(struct iovec) * count);
}

inline static void add_iov(void *buf, size_t len)
inline static void add_iov(const void *buf, size_t len)
{
iov_ensure(1);
add_iov_unsafe(buf, len);
Expand Down
36 changes: 0 additions & 36 deletions include/iproto.h
Expand Up @@ -38,40 +38,4 @@ struct iproto_interactor

void iproto_interact(void *);

#define ERROR_CODES(_) \
_(ERR_CODE_OK, 0x00000000, "ok") \
_(ERR_CODE_NONMASTER, 0x00000102, "non master connection, but it should be") \
_(ERR_CODE_ILLEGAL_PARAMS, 0x00000202, "illegal parametrs") \
_(ERR_CODE_BAD_UID, 0x00000302, "uid not from this storage range") \
_(ERR_CODE_NODE_IS_RO, 0x00000401, "node is marked as read-only") \
_(ERR_CODE_NODE_IS_NOT_LOCKED, 0x00000501, "node isn't locked") \
_(ERR_CODE_NODE_IS_LOCKED, 0x00000601, "node is locked") \
_(ERR_CODE_MEMORY_ISSUE, 0x00000701, "some memory issues") \
_(ERR_CODE_BAD_INTEGRITY, 0x00000802, "bad graph integrity") \
_(ERR_CODE_UNSUPPORTED_COMMAND, 0x00000a02, "unsupported command") \
/* gap due to silverproxy */ \
_(ERR_CODE_CANNOT_REGISTER, 0x00001801, "can't register new user") \
_(ERR_CODE_CANNOT_INIT_ALERT_ID, 0x00001a01, "can't generate alert id") \
_(ERR_CODE_CANNOT_DEL, 0x00001b02, "can't del node") \
_(ERR_CODE_USER_NOT_REGISTERED, 0x00001c02, "user isn't registered") \
/* silversearch error codes */ \
_(ERR_CODE_SYNTAX_ERROR, 0x00001d02, "syntax error in query") \
_(ERR_CODE_WRONG_FIELD, 0x00001e02, "unknown field") \
_(ERR_CODE_WRONG_NUMBER, 0x00001f02, "number value is out of range") \
_(ERR_CODE_DUPLICATE, 0x00002002, "insert already existing object") \
_(ERR_CODE_UNSUPPORTED_ORDER, 0x00002202, "can not order result") \
_(ERR_CODE_MULTIWRITE, 0x00002302, "multiple to update/delete") \
_(ERR_CODE_NOTHING, 0x00002400, "nothing to do (not an error)") \
_(ERR_CODE_UPDATE_ID, 0x00002502, "id's update") \
_(ERR_CODE_WRONG_VERSION, 0x00002602, "unsupported version of protocol") \
/* other generic error codes */ \
_(ERR_CODE_UNKNOWN_ERROR, 0x00002702, "unknown error") \
_(ERR_CODE_NODE_NOT_FOUND, 0x00003102, "node isn't found") \
_(ERR_CODE_NODE_FOUND, 0x00003702, "node is found") \
_(ERR_CODE_INDEX_VIOLATION, 0x00003802, "some index violation occur") \
_(ERR_CODE_NO_SUCH_NAMESPACE, 0x00003902, "there is no such namespace")

ENUM(error_codes, ERROR_CODES);
extern char *error_codes_strs[];
extern char *error_codes_desc_strs[];
#endif
9 changes: 4 additions & 5 deletions include/util.h
Expand Up @@ -35,14 +35,13 @@
#endif

/* Macros to define enum and corresponding strings. */
#define ENUM_MEMBER(s, v, d...) s = v,
#define ENUM_STRS_MEMBER(s, v, d...) [s] = #s,
#define ENUM_DESC_STRS_MEMBER(s, v, d...) [s] = d,
#define ENUM0_MEMBER(s, ...) s,
#define ENUM_MEMBER(s, v, ...) s = v,
#define ENUM_STRS_MEMBER(s, v, ...) [s] = #s,
#define ENUM0(enum_name, enum_members) enum enum_name {enum_members(ENUM0_MEMBER) enum_name##_MAX}
#define ENUM(enum_name, enum_members) enum enum_name {enum_members(ENUM_MEMBER) enum_name##_MAX}
#define STRS(enum_name, enum_members) \
char *enum_name##_strs[enum_name##_MAX + 1] = {enum_members(ENUM_STRS_MEMBER) '\0'}
#define DESC_STRS(enum_name, enum_members) \
char *enum_name##_desc_strs[enum_name##_MAX + 1] = {enum_members(ENUM_DESC_STRS_MEMBER) '\0'}

// Macros for printf functions
#include <inttypes.h>
Expand Down
1 change: 1 addition & 0 deletions mod/silverbox/box.c
Expand Up @@ -29,6 +29,7 @@
#include <stdbool.h>
#include <errno.h>

#include <errcode.h>
#include <fiber.h>
#include <iproto.h>
#include <log_io.h>
Expand Down
10 changes: 5 additions & 5 deletions mod/silverbox/box.h
Expand Up @@ -118,11 +118,11 @@ enum box_mode {

ENUM(messages, MESSAGES);

#define box_raise(n, err) \
({ \
if (n != ERR_CODE_NODE_IS_RO) \
say_warn("%s/%s", error_codes_strs[(n)], err); \
raise(n, err); \
#define box_raise(n, err) \
({ \
if (n != ERR_CODE_NODE_IS_RO) \
say_warn("%s/%s", ERRCODE_STR(error_codes, n), err); \
raise(n, err); \
})

struct box_txn *txn_alloc(u32 flags);
Expand Down
2 changes: 1 addition & 1 deletion mod/silverbox/index.c
Expand Up @@ -29,8 +29,8 @@
#include <stdbool.h>
#include <errno.h>

#include <errcode.h>
#include <fiber.h>
#include <iproto.h>
#include <log_io.h>
#include <pickle.h>
#include <salloc.h>
Expand Down
18 changes: 9 additions & 9 deletions mod/silverbox/memcached.c
Expand Up @@ -32,7 +32,7 @@
#include <unistd.h>
#include <stdlib.h>

#include <iproto.h>
#include <errcode.h>
#include <salloc.h>
#include <palloc.h>
#include <fiber.h>
Expand Down Expand Up @@ -260,8 +260,8 @@ memcached_dispatch(struct box_txn *txn)
add_iov("STORED\r\n", 8); \
} else { \
add_iov("SERVER_ERROR ", 13); \
add_iov(error_codes_desc_strs[ret_code], \
strlen(error_codes_desc_strs[ret_code])); \
add_iov(ERRCODE_DESC(error_codes, ret_code), \
strlen(ERRCODE_DESC(error_codes, ret_code))); \
add_iov("\r\n", 2); \
} \
} \
Expand Down Expand Up @@ -1078,8 +1078,8 @@ case 12:
add_iov("DELETED\r\n", 9);
else {
add_iov("SERVER_ERROR ", 13);
add_iov(error_codes_desc_strs[ret_code],
strlen(error_codes_desc_strs[ret_code]));
add_iov(ERRCODE_DESC(error_codes, ret_code),
strlen(ERRCODE_DESC(error_codes,ret_code)));
add_iov("\r\n", 2);
}
}
Expand Down Expand Up @@ -1112,8 +1112,8 @@ case 12:
add_iov("DELETED\r\n", 9);
else {
add_iov("SERVER_ERROR ", 13);
add_iov(error_codes_desc_strs[ret_code],
strlen(error_codes_desc_strs[ret_code]));
add_iov(ERRCODE_DESC(error_codes, ret_code),
strlen(ERRCODE_DESC(error_codes,ret_code)));
add_iov("\r\n", 2);
}
}
Expand Down Expand Up @@ -1142,8 +1142,8 @@ case 12:
add_iov("DELETED\r\n", 9);
else {
add_iov("SERVER_ERROR ", 13);
add_iov(error_codes_desc_strs[ret_code],
strlen(error_codes_desc_strs[ret_code]));
add_iov(ERRCODE_DESC(error_codes, ret_code),
strlen(ERRCODE_DESC(error_codes,ret_code)));
add_iov("\r\n", 2);
}
}
Expand Down
10 changes: 5 additions & 5 deletions mod/silverbox/memcached.rl
Expand Up @@ -30,7 +30,7 @@
#include <unistd.h>
#include <stdlib.h>

#include <iproto.h>
#include <errcode.h>
#include <salloc.h>
#include <palloc.h>
#include <fiber.h>
Expand Down Expand Up @@ -251,8 +251,8 @@ memcached_dispatch(struct box_txn *txn)
add_iov("STORED\r\n", 8); \
} else { \
add_iov("SERVER_ERROR ", 13); \
add_iov(error_codes_desc_strs[ret_code], \
strlen(error_codes_desc_strs[ret_code])); \
add_iov(ERRCODE_DESC(error_codes, ret_code), \
strlen(ERRCODE_DESC(error_codes, ret_code))); \
add_iov("\r\n", 2); \
} \
} \
Expand Down Expand Up @@ -382,8 +382,8 @@ memcached_dispatch(struct box_txn *txn)
add_iov("DELETED\r\n", 9);
else {
add_iov("SERVER_ERROR ", 13);
add_iov(error_codes_desc_strs[ret_code],
strlen(error_codes_desc_strs[ret_code]));
add_iov(ERRCODE_DESC(error_codes, ret_code),
strlen(ERRCODE_DESC(error_codes,ret_code)));
add_iov("\r\n", 2);
}
}
Expand Down

0 comments on commit 72bfb7f

Please sign in to comment.