src/lib/error.c
branchnew-lib-errors
changeset 218 5229a5d098b2
parent 217 7728d6ec3abf
--- a/src/lib/error.c	Wed May 27 23:57:48 2009 +0300
+++ b/src/lib/error.c	Thu May 28 00:35:02 2009 +0300
@@ -1,13 +1,9 @@
 #include "error.h"
 #include "log.h"
+#include "str.h"
 
-const struct error_list general_errors = ERROR_LIST("general",
-    ERROR_TYPE(         ERR_MEM,                "memory allocation error"           ),
-    ERROR_TYPE_STRING(  ERR_NOT_IMPLEMENTED,    "function not implmented"           ),
-    ERROR_TYPE_STRING(  ERR_MISC,               "miscellaneous error"               ),
-    ERROR_TYPE_STRING(  ERR_CMD_OPT,            "invalid command line option"       ),
-    ERROR_TYPE(         ERR_UNKNOWN,            "unknown error"                     )
-);
+#include <string.h>
+#include <stdlib.h>
 
 const struct error_type* error_lookup_code (const struct error_list *list, err_t code)
 {
@@ -26,8 +22,9 @@
 
 const struct error_type* error_lookup (const error_t *err)
 {
-    struct error_item *item;
-    struct error_type *type = NULL, *list;
+    const struct error_list *list = NULL;
+    const struct error_item *item;
+    const struct error_type *type;
 
     // traverse stack
     for (item = err->cur; item && item >= err->stack; item--) {
@@ -51,9 +48,9 @@
     return type;
 }
 
-const char* error_name (const struct error_list *list, err_t code);
+const char* error_name (const struct error_list *list, err_t code)
 {
-    struct error_type *type;
+    const struct error_type *type;
     
     // just do a lookup
     if ((type = error_lookup_code(list, code)))
@@ -71,8 +68,9 @@
     char *buf_ptr = buf; 
     size_t buf_size = sizeof(buf);
 
-    struct error_item *item;
-    struct error_type *type;
+    const struct error_list *list = NULL;
+    const struct error_item *item;
+    const struct error_type *type;
 
     // traverse stack
     for (item = err->cur; item && item >= err->stack; item--) {
@@ -92,7 +90,7 @@
         } else {
             // found code's type
             // delimit using colons, except at the end
-            buf_ptr += str_advance(&buf_size, NULL, str_append(buf_ptr, buf_size, "[%s] %s%s", 
+            buf_ptr += str_advance(&buf_size, NULL, str_append_fmt(buf_ptr, buf_size, "[%s] %s%s", 
                 list->name, type->name, item == err->stack ? "" : ": "
             ));
 
@@ -106,13 +104,13 @@
         // type mismatch
         buf_ptr += str_advance(&buf_size, NULL, str_append_fmt(buf_ptr, buf_size, ": [error_extra type mismatch: %s <=> %s",
             err->extra_type ? err->extra_type->name : NULL,
-            item->extra_type ? item->extra_type->name : NULL
+            type->extra_type ? type->extra_type->name : NULL
         ));
 
     } else if (err->extra_type) {
         // add extra info
         buf_ptr += str_advance(&buf_size, NULL, str_append_fmt(buf_ptr, buf_size, ": %s", 
-            err->extra_type->msg_func(item->extra_type, &item->extra_value)
+            err->extra_type->msg_func(err->extra_type, &err->extra_value)
         ));
 
     }
@@ -143,7 +141,7 @@
         // initial position
         return (err->cur = err->stack);
 
-    else if (err->cur < err->stack + ERR_DEPTH_MAX)
+    else if (err->cur < err->stack + ERROR_DEPTH_MAX)
         return (err->cur++);
 
     else
@@ -199,7 +197,7 @@
 
 void error_copy (error_t *dst, const error_t *src)
 {
-    struct error_item *item;
+    const struct error_item *item;
 
     // reset
     error_reset(dst);
@@ -228,12 +226,15 @@
     va_end(vargs);
 }
 
-void _error_abort (const char *file, const char *line, const char *func, const char *fmt, ...)
+void _error_abort (const char *file, int line, const char *func, const char *fmt, ...)
 {
     va_list vargs;
 
     va_start(vargs, fmt);
     _error_abort_(fmt, vargs, "%s:%d[%s]", file, line, func);
     va_end(vargs);
+
+    // then remember to die
+    abort();
 }