Add secondary text to errors
authorJ. Ali Harlow <ali@juiblex.co.uk>
Wed, 20 Aug 2014 17:53:37 +0000 (18:53 +0100)
committerJ. Ali Harlow <ali@juiblex.co.uk>
Wed, 20 Aug 2014 17:57:59 +0000 (18:57 +0100)
librazor/atomic.c
librazor/error.c
librazor/razor-internal.h
librazor/razor.h
librazor/root.c

index 028d57e..88a5d2e 100644 (file)
@@ -53,6 +53,12 @@ razor_allow_all_root_names(void)
        return allow_all_root_names;
 }
 
+RAZOR_EXPORT struct razor_error *
+razor_atomic_get_error(struct razor_atomic *atomic)
+{
+       return atomic->error;
+}
+
 RAZOR_EXPORT const char *
 razor_atomic_get_error_msg(struct razor_atomic *atomic)
 {
index c46241c..c847663 100644 (file)
 #include "razor.h"
 #include "razor-internal.h"
 
+const char *razor_error_get_path_str(struct razor_error *error)
+{
+       if (error->path_str)
+               return error->path_str;
+
+       if (error->path) {
+               error->path_str = razor_concat(error->path, ": ",
+                                              error->str, NULL);
+               return error->path_str;
+       }
+
+       return error->str;
+}
+
+/**
+ * razor_error_get_primary_text:
+ *
+ * Retrieves the basic information about an error. If a summary has been set
+ * then this will be returned. Otherwise the error string possibly prefixed
+ * by the path will be returned instead.
+ *
+ * Returns: Primary text of error.
+ **/
+RAZOR_EXPORT const char *
+razor_error_get_primary_text(struct razor_error *error)
+{
+       if (error->summary)
+               return error->summary;
+
+       return razor_error_get_path_str(error);
+}
+
+/**
+ * razor_error_get_secondary_text:
+ *
+ * Retrieves more detailed information about an error, if any. If a summary
+ * has been set the error string possibly prefixed by the path will be
+ * returned. Otherwise NULL will be returned.
+ *
+ * Returns: Secondary text of error or NULL.
+ **/
+RAZOR_EXPORT const char *
+razor_error_get_secondary_text(struct razor_error *error)
+{
+       if (!error->summary)
+               return NULL;
+
+       return razor_error_get_path_str(error);
+}
+
 RAZOR_EXPORT const char *
 razor_error_get_msg(struct razor_error *error)
 {
-       if (!error->msg) {
-               if (error->path)
-                       error->msg = razor_concat(error->path, ": ", error->str,
-                                                 NULL);
-               else
-                       error->msg = strdup(error->str);
-       }
+       const char *primary, *secondary;
+
+       if (error->msg)
+               return error->msg;
+
+       primary = razor_error_get_primary_text(error);
 
-       return error->msg;
+       secondary = razor_error_get_secondary_text(error);
+
+       if (secondary) {
+               error->msg = razor_concat(primary, "\n", secondary, NULL);
+               return error->msg;
+       } else
+               return primary;
 }
 
 #ifdef MSWIN_API
@@ -94,11 +149,34 @@ razor_error_new_str(const char *path, const char *str)
        return error;
 }
 
+RAZOR_EXPORT struct razor_error *
+razor_error_dup(struct razor_error *src, const char *summary)
+{
+       struct razor_error *error;
+
+       error = zalloc(sizeof *error);
+
+       if (summary)
+               error->summary = strdup(summary);
+       else if (src->summary)
+               error->summary = strdup(src->summary);
+
+       if (src->path)
+               error->path = strdup(src->path);
+
+       if (src->str)
+               error->str = strdup(src->str);
+
+       return error;
+}
+
 RAZOR_EXPORT void
 razor_error_free(struct razor_error *error)
 {
        free(error->path);
        free(error->str);
+       free(error->path_str);
+       free(error->summary);
        free(error->msg);
        free(error);
 }
index bf2517f..05204ec 100644 (file)
@@ -249,6 +249,8 @@ wchar_t *razor_utf8_to_utf16(const char *utf8, int len);
 struct razor_error {
        char *path;
        char *str;
+       char *path_str;
+       char *summary;
        char *msg;
 };
 
index 5c952bb..3277d03 100644 (file)
@@ -115,12 +115,21 @@ enum razor_set_flags {
 struct razor_error;
 
 struct razor_error *razor_error_new_str(const char *path, const char *str);
+struct razor_error *razor_error_dup(struct razor_error *src,
+                                   const char *summary);
 
 #define razor_set_error(error, path, str) \
        if (error) \
                *(error) = razor_error_new_str(path, str); \
        else
 
+#define razor_propagate_error(dest, src, summary) \
+       if (dest) \
+               *(dest) = razor_error_dup(src, summary); \
+       else
+
+const char *razor_error_get_primary_text(struct razor_error *error);
+const char *razor_error_get_secondary_text(struct razor_error *error);
 const char *razor_error_get_msg(struct razor_error *error);
 void razor_error_free(struct razor_error *error);
 
@@ -145,6 +154,7 @@ struct razor_atomic;
 
 struct razor_atomic *razor_atomic_open(const char *description);
 int razor_atomic_commit(struct razor_atomic *atomic);
+struct razor_error *razor_atomic_get_error(struct razor_atomic *atomic);
 const char *razor_atomic_get_error_msg(struct razor_atomic *atomic);
 void razor_atomic_destroy(struct razor_atomic *atomic);
 
index f3992d3..4ec5e49 100644 (file)
@@ -120,8 +120,9 @@ razor_root_create(const char *root, struct razor_error **error)
        free(file);
        retval = razor_atomic_commit(atomic);
        if (retval)
-               razor_set_error(error, NULL,
-                               "Could not write initial package set");
+               razor_propagate_error(error,
+                                     razor_atomic_get_error(atomic),
+                                     "Could not write initial package set");
        razor_set_unref(set);
        razor_atomic_destroy(atomic);