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