# HG changeset patch # User J. Ali Harlow # Date 1408557479 -3600 # Node ID f28bb31024b4f45dbf3bb461850fdb721e894753 # Parent fab0b8a61dcb91000a75c5229773388ace3fab25 Add secondary text to errors diff -r fab0b8a61dcb -r f28bb31024b4 librazor/atomic.c --- a/librazor/atomic.c Wed Aug 20 18:57:52 2014 +0100 +++ b/librazor/atomic.c Wed Aug 20 18:57:59 2014 +0100 @@ -53,6 +53,12 @@ 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 -r fab0b8a61dcb -r f28bb31024b4 librazor/error.c --- a/librazor/error.c Wed Aug 20 18:57:52 2014 +0100 +++ b/librazor/error.c Wed Aug 20 18:57:59 2014 +0100 @@ -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; - return error->msg; + if (error->msg) + return error->msg; + + primary = razor_error_get_primary_text(error); + + 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 @@ 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 -r fab0b8a61dcb -r f28bb31024b4 librazor/razor-internal.h --- a/librazor/razor-internal.h Wed Aug 20 18:57:52 2014 +0100 +++ b/librazor/razor-internal.h Wed Aug 20 18:57:59 2014 +0100 @@ -249,6 +249,8 @@ struct razor_error { char *path; char *str; + char *path_str; + char *summary; char *msg; }; diff -r fab0b8a61dcb -r f28bb31024b4 librazor/razor.h --- a/librazor/razor.h Wed Aug 20 18:57:52 2014 +0100 +++ b/librazor/razor.h Wed Aug 20 18:57:59 2014 +0100 @@ -115,12 +115,21 @@ 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 *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 -r fab0b8a61dcb -r f28bb31024b4 librazor/root.c --- a/librazor/root.c Wed Aug 20 18:57:52 2014 +0100 +++ b/librazor/root.c Wed Aug 20 18:57:59 2014 +0100 @@ -120,8 +120,9 @@ 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);