librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jun 08 18:02:49 2018 +0100 (2018-06-08)
changeset 501 850be6a6885c
parent 455 df914f383f5c
permissions -rw-r--r--
Added tag 0.7 for changeset f98d77376544
     1 /*
     2  * Copyright (C) 2011-2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #ifdef MSWIN_API
    22 #include <windows.h>
    23 #ifndef ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED
    24 #define ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED	6832L
    25 #endif
    26 #endif
    27 #include <stdlib.h>
    28 #include <string.h>
    29 
    30 #include "razor.h"
    31 #include "razor-internal.h"
    32 
    33 RAZOR_EXPORT int
    34 razor_error_get_domain(struct razor_error *error)
    35 {
    36 	return error->domain;
    37 }
    38 
    39 RAZOR_EXPORT int
    40 razor_error_get_code(struct razor_error *error)
    41 {
    42 	return error->code;
    43 }
    44 
    45 void
    46 razor_error_set_object(struct razor_error *error, const char *object)
    47 {
    48 	if (!error)
    49 		return;
    50 
    51 	free(error->object);
    52 
    53 	if (object)
    54 		error->object = strdup(object);
    55 	else
    56 		error->object = NULL;
    57 
    58 	if (error->obj_str) {
    59 		free(error->obj_str);
    60 		error->obj_str = NULL;
    61 	}
    62 
    63 	if (error->msg) {
    64 		free(error->msg);
    65 		error->msg = NULL;
    66 	}
    67 }
    68 
    69 RAZOR_EXPORT const char *
    70 razor_error_get_object(struct razor_error *error)
    71 {
    72 	return error->object;
    73 }
    74 
    75 static const char *razor_error_get_obj_str(struct razor_error *error)
    76 {
    77 	if (error->obj_str)
    78 		return error->obj_str;
    79 
    80 	if (error->object) {
    81 		error->obj_str = razor_concat(error->object, ": ",
    82 					      error->str, NULL);
    83 		return error->obj_str;
    84 	}
    85 
    86 	return error->str;
    87 }
    88 
    89 /**
    90  * razor_error_get_primary_text:
    91  *
    92  * Retrieves the basic information about an error. If a summary has been set
    93  * then this will be returned. Otherwise the error string possibly prefixed
    94  * by the object will be returned instead.
    95  *
    96  * Returns: Primary text of error.
    97  **/
    98 RAZOR_EXPORT const char *
    99 razor_error_get_primary_text(struct razor_error *error)
   100 {
   101 	if (error->summary)
   102 		return error->summary;
   103 
   104 	return razor_error_get_obj_str(error);
   105 }
   106 
   107 /**
   108  * razor_error_get_secondary_text:
   109  *
   110  * Retrieves more detailed information about an error, if any. If a summary
   111  * has been set the error string possibly prefixed by the object will be
   112  * returned. Otherwise NULL will be returned.
   113  *
   114  * Returns: Secondary text of error or NULL.
   115  **/
   116 RAZOR_EXPORT const char *
   117 razor_error_get_secondary_text(struct razor_error *error)
   118 {
   119 	if (!error->summary)
   120 		return NULL;
   121 
   122 	return razor_error_get_obj_str(error);
   123 }
   124 
   125 RAZOR_EXPORT const char *
   126 razor_error_get_msg(struct razor_error *error)
   127 {
   128 	const char *primary, *secondary;
   129 
   130 	if (error->msg)
   131 		return error->msg;
   132 
   133 	primary = razor_error_get_primary_text(error);
   134 
   135 	secondary = razor_error_get_secondary_text(error);
   136 
   137 	if (secondary) {
   138 		error->msg = razor_concat(primary, "\n", secondary, NULL);
   139 		return error->msg;
   140 	} else
   141 		return primary;
   142 }
   143 
   144 #ifdef MSWIN_API
   145 struct razor_error *
   146 razor_error_new_mswin(const wchar_t *object, DWORD err)
   147 {
   148 	struct razor_error *error;
   149 	wchar_t *buf;
   150 
   151 	error = zalloc(sizeof *error);
   152 
   153 	error->domain = RAZOR_MSWIN_ERROR;
   154 	error->code = err;
   155 
   156 	if (object)
   157 		error->object = razor_utf16_to_utf8(object, -1);
   158 
   159 	switch(err) {
   160 	case ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED:
   161 		/*
   162 		 * Attempting to include files in a transaction on a filesystem
   163 		 * that doesn't support them (only NTFS?) produces this error
   164 		 * for which the default text isn't very informative. Try and
   165 		 * give more useful information.
   166 		 */
   167 		error->str = strdup("Not allowed (perhaps the filesystem "
   168 				    "doesn't support transactions)");
   169 		break;
   170 	default:
   171 		FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
   172 			       FORMAT_MESSAGE_FROM_SYSTEM|
   173 			       FORMAT_MESSAGE_IGNORE_INSERTS,
   174 			       NULL, err,
   175 			       MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
   176 			       (LPWSTR)&buf, 0, NULL);
   177 		error->str = razor_utf16_to_utf8(buf, -1);
   178 		LocalFree(buf);
   179 		break;
   180 	}
   181 
   182 	return error;
   183 }
   184 
   185 struct razor_error *
   186 razor_error_new_str2(int domain, int code, const wchar_t *object,
   187 		     const char *str)
   188 {
   189 	struct razor_error *error;
   190 
   191 	error = zalloc(sizeof *error);
   192 
   193 	error->domain = domain;
   194 	error->code = code;
   195 
   196 	if (object)
   197 		error->object = razor_utf16_to_utf8(object, -1);
   198 
   199 	error->str = strdup(str);
   200 
   201 	return error;
   202 }
   203 #endif	/* MSWIN_API */
   204 
   205 RAZOR_EXPORT struct razor_error *
   206 razor_error_new_str(int domain, int code, const char *object, const char *str)
   207 {
   208 	struct razor_error *error;
   209 
   210 	error = zalloc(sizeof *error);
   211 
   212 	error->domain = domain;
   213 	error->code = code;
   214 
   215 	if (object)
   216 		error->object = strdup(object);
   217 
   218 	error->str = strdup(str);
   219 
   220 	return error;
   221 }
   222 
   223 RAZOR_EXPORT struct razor_error *
   224 razor_error_dup(struct razor_error *src, const char *summary)
   225 {
   226 	struct razor_error *error;
   227 
   228 	error = zalloc(sizeof *error);
   229 
   230 	error->domain = src->domain;
   231 	error->code = src->code;
   232 
   233 	if (summary)
   234 		error->summary = strdup(summary);
   235 	else if (src->summary)
   236 		error->summary = strdup(src->summary);
   237 
   238 	if (src->object)
   239 		error->object = strdup(src->object);
   240 
   241 	if (src->str)
   242 		error->str = strdup(src->str);
   243 
   244 	return error;
   245 }
   246 
   247 RAZOR_EXPORT void
   248 razor_error_free(struct razor_error *error)
   249 {
   250 	free(error->object);
   251 	free(error->str);
   252 	free(error->obj_str);
   253 	free(error->summary);
   254 	free(error->msg);
   255 	free(error);
   256 }