librazor/error.c
changeset 440 48204dea0b9f
parent 424 8cbc438cc298
child 442 c4bcba8023a9
     1.1 --- a/librazor/error.c	Thu Feb 16 17:33:47 2012 +0000
     1.2 +++ b/librazor/error.c	Sat Aug 23 11:13:48 2014 +0100
     1.3 @@ -27,18 +27,73 @@
     1.4  #include "razor.h"
     1.5  #include "razor-internal.h"
     1.6  
     1.7 +const char *razor_error_get_path_str(struct razor_error *error)
     1.8 +{
     1.9 +	if (error->path_str)
    1.10 +		return error->path_str;
    1.11 +
    1.12 +	if (error->path) {
    1.13 +		error->path_str = razor_concat(error->path, ": ",
    1.14 +					       error->str, NULL);
    1.15 +		return error->path_str;
    1.16 +	}
    1.17 +
    1.18 +	return error->str;
    1.19 +}
    1.20 +
    1.21 +/**
    1.22 + * razor_error_get_primary_text:
    1.23 + *
    1.24 + * Retrieves the basic information about an error. If a summary has been set
    1.25 + * then this will be returned. Otherwise the error string possibly prefixed
    1.26 + * by the path will be returned instead.
    1.27 + *
    1.28 + * Returns: Primary text of error.
    1.29 + **/
    1.30 +RAZOR_EXPORT const char *
    1.31 +razor_error_get_primary_text(struct razor_error *error)
    1.32 +{
    1.33 +	if (error->summary)
    1.34 +		return error->summary;
    1.35 +
    1.36 +	return razor_error_get_path_str(error);
    1.37 +}
    1.38 +
    1.39 +/**
    1.40 + * razor_error_get_secondary_text:
    1.41 + *
    1.42 + * Retrieves more detailed information about an error, if any. If a summary
    1.43 + * has been set the error string possibly prefixed by the path will be
    1.44 + * returned. Otherwise NULL will be returned.
    1.45 + *
    1.46 + * Returns: Secondary text of error or NULL.
    1.47 + **/
    1.48 +RAZOR_EXPORT const char *
    1.49 +razor_error_get_secondary_text(struct razor_error *error)
    1.50 +{
    1.51 +	if (!error->summary)
    1.52 +		return NULL;
    1.53 +
    1.54 +	return razor_error_get_path_str(error);
    1.55 +}
    1.56 +
    1.57  RAZOR_EXPORT const char *
    1.58  razor_error_get_msg(struct razor_error *error)
    1.59  {
    1.60 -	if (!error->msg) {
    1.61 -		if (error->path)
    1.62 -			error->msg = razor_concat(error->path, ": ", error->str,
    1.63 -						  NULL);
    1.64 -		else
    1.65 -			error->msg = strdup(error->str);
    1.66 -	}
    1.67 +	const char *primary, *secondary;
    1.68  
    1.69 -	return error->msg;
    1.70 +	if (error->msg)
    1.71 +		return error->msg;
    1.72 +
    1.73 +	primary = razor_error_get_primary_text(error);
    1.74 +
    1.75 +	secondary = razor_error_get_secondary_text(error);
    1.76 +
    1.77 +	if (secondary) {
    1.78 +		error->msg = razor_concat(primary, "\n", secondary, NULL);
    1.79 +		return error->msg;
    1.80 +	} else
    1.81 +		return primary;
    1.82  }
    1.83  
    1.84  #ifdef MSWIN_API
    1.85 @@ -94,11 +149,34 @@
    1.86  	return error;
    1.87  }
    1.88  
    1.89 +RAZOR_EXPORT struct razor_error *
    1.90 +razor_error_dup(struct razor_error *src, const char *summary)
    1.91 +{
    1.92 +	struct razor_error *error;
    1.93 +
    1.94 +	error = zalloc(sizeof *error);
    1.95 +
    1.96 +	if (summary)
    1.97 +		error->summary = strdup(summary);
    1.98 +	else if (src->summary)
    1.99 +		error->summary = strdup(src->summary);
   1.100 +
   1.101 +	if (src->path)
   1.102 +		error->path = strdup(src->path);
   1.103 +
   1.104 +	if (src->str)
   1.105 +		error->str = strdup(src->str);
   1.106 +
   1.107 +	return error;
   1.108 +}
   1.109 +
   1.110  RAZOR_EXPORT void
   1.111  razor_error_free(struct razor_error *error)
   1.112  {
   1.113  	free(error->path);
   1.114  	free(error->str);
   1.115 +	free(error->path_str);
   1.116 +	free(error->summary);
   1.117  	free(error->msg);
   1.118  	free(error);
   1.119  }