Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

distinguish between different error types

This is write-only within Whistlepig, but will be useful for
language bindings.
  • Loading branch information...
commit 85f7efa7556a4a3a07591c6f39398613b5701ac0 1 parent cf2c641
@wmorgan authored
Showing with 21 additions and 11 deletions.
  1. +2 −1  error.c
  2. +17 −7 error.h
  3. +1 −2  index.c
  4. +1 −1  segment.c
View
3  error.c
@@ -1,9 +1,10 @@
#include <stdlib.h>
#include "error.h"
-wp_error* wp_error_new(const char* msg, const char* src) {
+wp_error* wp_error_new(const char* msg, const char* src, unsigned char type) {
wp_error* ret = malloc(sizeof(wp_error));
ret->msg = msg;
+ ret->type = type;
ret->size = 1;
ret->srcs = malloc(sizeof(const char*));
ret->srcs[0] = src;
View
24 error.h
@@ -25,8 +25,13 @@
#include <stdio.h>
#include <string.h>
+#define WP_ERROR_TYPE_BASIC 1
+#define WP_ERROR_TYPE_SYSTEM 2
+#define WP_ERROR_TYPE_VERSION 3
+
// pseudo-backtrace
typedef struct wp_error {
+ unsigned char type;
unsigned int size;
const char* msg;
const char** srcs;
@@ -39,25 +44,30 @@ typedef struct wp_error {
// API methods
// private: make a new error object with a message and source line
-wp_error* wp_error_new(const char* msg, const char* src) RAISES_ERROR;
+wp_error* wp_error_new(const char* msg, const char* src, unsigned char type) RAISES_ERROR;
// private: add a source line to a pre-existing error
wp_error* wp_error_chain(wp_error* e, const char* src) RAISES_ERROR;
// public: free an error, once handled
void wp_error_free(wp_error* e);
-// public: raise an error with a printf-style message
-#define RAISE_ERROR(fmt, ...) do { \
+// private: internal mechanics for raising an error
+#define RAISE_ERROR_OF_TYPE(type, fmt, ...) do { \
char* msg = malloc(1024); \
char* src = malloc(1024); \
snprintf(msg, 1024, fmt, ## __VA_ARGS__); \
snprintf(src, 1024, "%s (%s:%d)", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
- return wp_error_new(msg, src); \
+ return wp_error_new(msg, src, type); \
} while(0)
-// public: raise an error with a printf-style message and have strerror() autoamtically
-// appended
-#define RAISE_SYSERROR(fmt, ...) RAISE_ERROR(fmt ": %s", ## __VA_ARGS__, strerror(errno))
+// public: raise a basic error
+#define RAISE_ERROR(fmt, ...) RAISE_ERROR_OF_TYPE(WP_ERROR_TYPE_BASIC, fmt, ## __VA_ARGS__)
+
+// public: raise a version error
+#define RAISE_VERSION_ERROR(fmt, ...) RAISE_ERROR_OF_TYPE(WP_ERROR_TYPE_VERSION, fmt, ## __VA_ARGS__)
+
+// public: raise a system error with strerror() automatically appended to the message
+#define RAISE_SYSERROR(fmt, ...) RAISE_ERROR_OF_TYPE(WP_ERROR_TYPE_SYSTEM, fmt ": %s", ## __VA_ARGS__, strerror(errno))
// public: relay an error up the stack if the called function returns one.
#define RELAY_ERROR(e) do { \
View
3  index.c
@@ -32,7 +32,6 @@ RAISING_STATIC(release_lock(wp_index* index)) {
return NO_ERROR;
}
-
RAISING_STATIC(index_info_init(index_info* ii, uint32_t index_version)) {
ii->index_version = index_version;
ii->num_segments = 0;
@@ -42,7 +41,7 @@ RAISING_STATIC(index_info_init(index_info* ii, uint32_t index_version)) {
}
RAISING_STATIC(index_info_validate(index_info* ii, uint32_t index_version)) {
- if(ii->index_version != index_version) RAISE_ERROR("index has type %u; expecting type %u", ii->index_version, index_version);
+ if(ii->index_version != index_version) RAISE_VERSION_ERROR("index has type %u; expecting type %u", ii->index_version, index_version);
return NO_ERROR;
}
View
2  segment.c
@@ -64,7 +64,7 @@ RAISING_STATIC(segment_info_init(segment_info* si, uint32_t segment_version)) {
}
RAISING_STATIC(segment_info_validate(segment_info* si, uint32_t segment_version)) {
- if(si->segment_version != segment_version) RAISE_ERROR("segment has type %u; expecting type %u", si->segment_version, segment_version);
+ if(si->segment_version != segment_version) RAISE_VERSION_ERROR("segment has type %u; expecting type %u", si->segment_version, segment_version);
return NO_ERROR;
}
Please sign in to comment.
Something went wrong with that request. Please try again.