#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
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);
}
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);
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);
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);