Skip to content

Commit

Permalink
distinguish between different error types
Browse files Browse the repository at this point in the history
This is write-only within Whistlepig, but will be useful for
language bindings.
  • Loading branch information
wmorgan committed Jun 9, 2012
1 parent cf2c641 commit 85f7efa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion error.c
@@ -1,9 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include "error.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)); wp_error* ret = malloc(sizeof(wp_error));
ret->msg = msg; ret->msg = msg;
ret->type = type;
ret->size = 1; ret->size = 1;
ret->srcs = malloc(sizeof(const char*)); ret->srcs = malloc(sizeof(const char*));
ret->srcs[0] = src; ret->srcs[0] = src;
Expand Down
24 changes: 17 additions & 7 deletions error.h
Expand Up @@ -25,8 +25,13 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>


#define WP_ERROR_TYPE_BASIC 1
#define WP_ERROR_TYPE_SYSTEM 2
#define WP_ERROR_TYPE_VERSION 3

// pseudo-backtrace // pseudo-backtrace
typedef struct wp_error { typedef struct wp_error {
unsigned char type;
unsigned int size; unsigned int size;
const char* msg; const char* msg;
const char** srcs; const char** srcs;
Expand All @@ -39,25 +44,30 @@ typedef struct wp_error {
// API methods // API methods


// private: make a new error object with a message and source line // 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 // private: add a source line to a pre-existing error
wp_error* wp_error_chain(wp_error* e, const char* src) RAISES_ERROR; wp_error* wp_error_chain(wp_error* e, const char* src) RAISES_ERROR;


// public: free an error, once handled // public: free an error, once handled
void wp_error_free(wp_error* e); void wp_error_free(wp_error* e);


// public: raise an error with a printf-style message // private: internal mechanics for raising an error
#define RAISE_ERROR(fmt, ...) do { \ #define RAISE_ERROR_OF_TYPE(type, fmt, ...) do { \
char* msg = malloc(1024); \ char* msg = malloc(1024); \
char* src = malloc(1024); \ char* src = malloc(1024); \
snprintf(msg, 1024, fmt, ## __VA_ARGS__); \ snprintf(msg, 1024, fmt, ## __VA_ARGS__); \
snprintf(src, 1024, "%s (%s:%d)", __PRETTY_FUNCTION__, __FILE__, __LINE__); \ 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) } while(0)


// public: raise an error with a printf-style message and have strerror() autoamtically // public: raise a basic error
// appended #define RAISE_ERROR(fmt, ...) RAISE_ERROR_OF_TYPE(WP_ERROR_TYPE_BASIC, fmt, ## __VA_ARGS__)
#define RAISE_SYSERROR(fmt, ...) RAISE_ERROR(fmt ": %s", ## __VA_ARGS__, strerror(errno))
// 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. // public: relay an error up the stack if the called function returns one.
#define RELAY_ERROR(e) do { \ #define RELAY_ERROR(e) do { \
Expand Down
3 changes: 1 addition & 2 deletions index.c
Expand Up @@ -32,7 +32,6 @@ RAISING_STATIC(release_lock(wp_index* index)) {
return NO_ERROR; return NO_ERROR;
} }



RAISING_STATIC(index_info_init(index_info* ii, uint32_t index_version)) { RAISING_STATIC(index_info_init(index_info* ii, uint32_t index_version)) {
ii->index_version = index_version; ii->index_version = index_version;
ii->num_segments = 0; ii->num_segments = 0;
Expand All @@ -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)) { 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; return NO_ERROR;
} }


Expand Down
2 changes: 1 addition & 1 deletion segment.c
Expand Up @@ -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)) { 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; return NO_ERROR;
} }


Expand Down

0 comments on commit 85f7efa

Please sign in to comment.