From b767bfec3a090af508cb681dd32ef4e76f8e0149 Mon Sep 17 00:00:00 2001 From: J. Ali Harlow Date: Wed, 20 Aug 2014 18:53:37 +0100 Subject: [PATCH] Add secondary text to errors --- librazor/atomic.c | 6 +++ librazor/error.c | 94 +++++++++++++++++++++++++++++++++++++++++---- librazor/razor-internal.h | 2 + librazor/razor.h | 10 +++++ librazor/root.c | 5 +- 5 files changed, 107 insertions(+), 10 deletions(-) diff --git a/librazor/atomic.c b/librazor/atomic.c index 028d57e..88a5d2e 100644 --- a/librazor/atomic.c +++ b/librazor/atomic.c @@ -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) { diff --git a/librazor/error.c b/librazor/error.c index c46241c..c847663 100644 --- a/librazor/error.c +++ b/librazor/error.c @@ -27,18 +27,73 @@ #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); } diff --git a/librazor/razor-internal.h b/librazor/razor-internal.h index bf2517f..05204ec 100644 --- a/librazor/razor-internal.h +++ b/librazor/razor-internal.h @@ -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; }; diff --git a/librazor/razor.h b/librazor/razor.h index 5c952bb..3277d03 100644 --- a/librazor/razor.h +++ b/librazor/razor.h @@ -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); diff --git a/librazor/root.c b/librazor/root.c index f3992d3..4ec5e49 100644 --- a/librazor/root.c +++ b/librazor/root.c @@ -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); -- 1.7.1