2 * Copyright (C) 2011-2012, 2014 J. Ali Harlow <ali@juiblex.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "razor-internal.h"
31 razor_error_get_domain(struct razor_error *error)
37 razor_error_get_code(struct razor_error *error)
42 RAZOR_EXPORT const char *
43 razor_error_get_object(struct razor_error *error)
48 static const char *razor_error_get_obj_str(struct razor_error *error)
51 return error->obj_str;
54 error->obj_str = razor_concat(error->object, ": ",
56 return error->obj_str;
63 * razor_error_get_primary_text:
65 * Retrieves the basic information about an error. If a summary has been set
66 * then this will be returned. Otherwise the error string possibly prefixed
67 * by the object will be returned instead.
69 * Returns: Primary text of error.
71 RAZOR_EXPORT const char *
72 razor_error_get_primary_text(struct razor_error *error)
75 return error->summary;
77 return razor_error_get_obj_str(error);
81 * razor_error_get_secondary_text:
83 * Retrieves more detailed information about an error, if any. If a summary
84 * has been set the error string possibly prefixed by the object will be
85 * returned. Otherwise NULL will be returned.
87 * Returns: Secondary text of error or NULL.
89 RAZOR_EXPORT const char *
90 razor_error_get_secondary_text(struct razor_error *error)
95 return razor_error_get_obj_str(error);
98 RAZOR_EXPORT const char *
99 razor_error_get_msg(struct razor_error *error)
101 const char *primary, *secondary;
106 primary = razor_error_get_primary_text(error);
108 secondary = razor_error_get_secondary_text(error);
111 error->msg = razor_concat(primary, "\n", secondary, NULL);
119 razor_error_new_mswin(const wchar_t *object, DWORD err)
121 struct razor_error *error;
124 error = zalloc(sizeof *error);
126 error->domain = RAZOR_MSWIN_ERROR;
130 error->object = razor_utf16_to_utf8(object, -1);
132 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
133 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
134 NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
135 (LPWSTR)&buf, 0, NULL);
136 error->str = razor_utf16_to_utf8(buf, -1);
143 razor_error_new_str2(int domain, int code, const wchar_t *object,
146 struct razor_error *error;
148 error = zalloc(sizeof *error);
150 error->domain = domain;
154 error->object = razor_utf16_to_utf8(object, -1);
156 error->str = strdup(str);
160 #endif /* MSWIN_API */
162 RAZOR_EXPORT struct razor_error *
163 razor_error_new_str(int domain, int code, const char *object, const char *str)
165 struct razor_error *error;
167 error = zalloc(sizeof *error);
169 error->domain = domain;
173 error->object = strdup(object);
175 error->str = strdup(str);
180 RAZOR_EXPORT struct razor_error *
181 razor_error_dup(struct razor_error *src, const char *summary)
183 struct razor_error *error;
185 error = zalloc(sizeof *error);
187 error->domain = src->domain;
188 error->code = src->code;
191 error->summary = strdup(summary);
192 else if (src->summary)
193 error->summary = strdup(src->summary);
196 error->object = strdup(src->object);
199 error->str = strdup(src->str);
205 razor_error_free(struct razor_error *error)
209 free(error->obj_str);
210 free(error->summary);