Add secondary text to errors
authorJ. Ali Harlow <ali@juiblex.co.uk>
Wed Aug 20 18:57:59 2014 +0100 (2014-08-20)
changeset 439f28bb31024b4
parent 438 fab0b8a61dcb
child 440 48204dea0b9f
Add secondary text to errors
librazor/atomic.c
librazor/error.c
librazor/razor-internal.h
librazor/razor.h
librazor/root.c
     1.1 --- a/librazor/atomic.c	Wed Aug 20 18:57:52 2014 +0100
     1.2 +++ b/librazor/atomic.c	Wed Aug 20 18:57:59 2014 +0100
     1.3 @@ -53,6 +53,12 @@
     1.4  	return allow_all_root_names;
     1.5  }
     1.6  
     1.7 +RAZOR_EXPORT struct razor_error *
     1.8 +razor_atomic_get_error(struct razor_atomic *atomic)
     1.9 +{
    1.10 +	return atomic->error;
    1.11 +}
    1.12 +
    1.13  RAZOR_EXPORT const char *
    1.14  razor_atomic_get_error_msg(struct razor_atomic *atomic)
    1.15  {
     2.1 --- a/librazor/error.c	Wed Aug 20 18:57:52 2014 +0100
     2.2 +++ b/librazor/error.c	Wed Aug 20 18:57:59 2014 +0100
     2.3 @@ -27,18 +27,73 @@
     2.4  #include "razor.h"
     2.5  #include "razor-internal.h"
     2.6  
     2.7 +const char *razor_error_get_path_str(struct razor_error *error)
     2.8 +{
     2.9 +	if (error->path_str)
    2.10 +		return error->path_str;
    2.11 +
    2.12 +	if (error->path) {
    2.13 +		error->path_str = razor_concat(error->path, ": ",
    2.14 +					       error->str, NULL);
    2.15 +		return error->path_str;
    2.16 +	}
    2.17 +
    2.18 +	return error->str;
    2.19 +}
    2.20 +
    2.21 +/**
    2.22 + * razor_error_get_primary_text:
    2.23 + *
    2.24 + * Retrieves the basic information about an error. If a summary has been set
    2.25 + * then this will be returned. Otherwise the error string possibly prefixed
    2.26 + * by the path will be returned instead.
    2.27 + *
    2.28 + * Returns: Primary text of error.
    2.29 + **/
    2.30 +RAZOR_EXPORT const char *
    2.31 +razor_error_get_primary_text(struct razor_error *error)
    2.32 +{
    2.33 +	if (error->summary)
    2.34 +		return error->summary;
    2.35 +
    2.36 +	return razor_error_get_path_str(error);
    2.37 +}
    2.38 +
    2.39 +/**
    2.40 + * razor_error_get_secondary_text:
    2.41 + *
    2.42 + * Retrieves more detailed information about an error, if any. If a summary
    2.43 + * has been set the error string possibly prefixed by the path will be
    2.44 + * returned. Otherwise NULL will be returned.
    2.45 + *
    2.46 + * Returns: Secondary text of error or NULL.
    2.47 + **/
    2.48 +RAZOR_EXPORT const char *
    2.49 +razor_error_get_secondary_text(struct razor_error *error)
    2.50 +{
    2.51 +	if (!error->summary)
    2.52 +		return NULL;
    2.53 +
    2.54 +	return razor_error_get_path_str(error);
    2.55 +}
    2.56 +
    2.57  RAZOR_EXPORT const char *
    2.58  razor_error_get_msg(struct razor_error *error)
    2.59  {
    2.60 -	if (!error->msg) {
    2.61 -		if (error->path)
    2.62 -			error->msg = razor_concat(error->path, ": ", error->str,
    2.63 -						  NULL);
    2.64 -		else
    2.65 -			error->msg = strdup(error->str);
    2.66 -	}
    2.67 +	const char *primary, *secondary;
    2.68  
    2.69 -	return error->msg;
    2.70 +	if (error->msg)
    2.71 +		return error->msg;
    2.72 +
    2.73 +	primary = razor_error_get_primary_text(error);
    2.74 +
    2.75 +	secondary = razor_error_get_secondary_text(error);
    2.76 +
    2.77 +	if (secondary) {
    2.78 +		error->msg = razor_concat(primary, "\n", secondary, NULL);
    2.79 +		return error->msg;
    2.80 +	} else
    2.81 +		return primary;
    2.82  }
    2.83  
    2.84  #ifdef MSWIN_API
    2.85 @@ -94,11 +149,34 @@
    2.86  	return error;
    2.87  }
    2.88  
    2.89 +RAZOR_EXPORT struct razor_error *
    2.90 +razor_error_dup(struct razor_error *src, const char *summary)
    2.91 +{
    2.92 +	struct razor_error *error;
    2.93 +
    2.94 +	error = zalloc(sizeof *error);
    2.95 +
    2.96 +	if (summary)
    2.97 +		error->summary = strdup(summary);
    2.98 +	else if (src->summary)
    2.99 +		error->summary = strdup(src->summary);
   2.100 +
   2.101 +	if (src->path)
   2.102 +		error->path = strdup(src->path);
   2.103 +
   2.104 +	if (src->str)
   2.105 +		error->str = strdup(src->str);
   2.106 +
   2.107 +	return error;
   2.108 +}
   2.109 +
   2.110  RAZOR_EXPORT void
   2.111  razor_error_free(struct razor_error *error)
   2.112  {
   2.113  	free(error->path);
   2.114  	free(error->str);
   2.115 +	free(error->path_str);
   2.116 +	free(error->summary);
   2.117  	free(error->msg);
   2.118  	free(error);
   2.119  }
     3.1 --- a/librazor/razor-internal.h	Wed Aug 20 18:57:52 2014 +0100
     3.2 +++ b/librazor/razor-internal.h	Wed Aug 20 18:57:59 2014 +0100
     3.3 @@ -249,6 +249,8 @@
     3.4  struct razor_error {
     3.5  	char *path;
     3.6  	char *str;
     3.7 +	char *path_str;
     3.8 +	char *summary;
     3.9  	char *msg;
    3.10  };
    3.11  
     4.1 --- a/librazor/razor.h	Wed Aug 20 18:57:52 2014 +0100
     4.2 +++ b/librazor/razor.h	Wed Aug 20 18:57:59 2014 +0100
     4.3 @@ -115,12 +115,21 @@
     4.4  struct razor_error;
     4.5  
     4.6  struct razor_error *razor_error_new_str(const char *path, const char *str);
     4.7 +struct razor_error *razor_error_dup(struct razor_error *src,
     4.8 +				    const char *summary);
     4.9  
    4.10  #define razor_set_error(error, path, str) \
    4.11  	if (error) \
    4.12  		*(error) = razor_error_new_str(path, str); \
    4.13  	else
    4.14  
    4.15 +#define razor_propagate_error(dest, src, summary) \
    4.16 +	if (dest) \
    4.17 +		*(dest) = razor_error_dup(src, summary); \
    4.18 +	else
    4.19 +
    4.20 +const char *razor_error_get_primary_text(struct razor_error *error);
    4.21 +const char *razor_error_get_secondary_text(struct razor_error *error);
    4.22  const char *razor_error_get_msg(struct razor_error *error);
    4.23  void razor_error_free(struct razor_error *error);
    4.24  
    4.25 @@ -145,6 +154,7 @@
    4.26  
    4.27  struct razor_atomic *razor_atomic_open(const char *description);
    4.28  int razor_atomic_commit(struct razor_atomic *atomic);
    4.29 +struct razor_error *razor_atomic_get_error(struct razor_atomic *atomic);
    4.30  const char *razor_atomic_get_error_msg(struct razor_atomic *atomic);
    4.31  void razor_atomic_destroy(struct razor_atomic *atomic);
    4.32  
     5.1 --- a/librazor/root.c	Wed Aug 20 18:57:52 2014 +0100
     5.2 +++ b/librazor/root.c	Wed Aug 20 18:57:59 2014 +0100
     5.3 @@ -120,8 +120,9 @@
     5.4  	free(file);
     5.5  	retval = razor_atomic_commit(atomic);
     5.6  	if (retval)
     5.7 -		razor_set_error(error, NULL,
     5.8 -				"Could not write initial package set");
     5.9 +		razor_propagate_error(error,
    5.10 +				      razor_atomic_get_error(atomic),
    5.11 +				      "Could not write initial package set");
    5.12  	razor_set_unref(set);
    5.13  	razor_atomic_destroy(atomic);
    5.14