librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Apr 14 11:59:56 2016 +0100 (2016-04-14)
changeset 466 bad1547191dc
parent 447 0a5e583393e1
child 499 c89e5edb8eae
permissions -rw-r--r--
Release 0.6.1
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@447
    45
RAZOR_EXPORT const char *
ali@447
    46
razor_error_get_object(struct razor_error *error)
ali@447
    47
{
ali@447
    48
	return error->object;
ali@447
    49
}
ali@447
    50
ali@447
    51
static const char *razor_error_get_obj_str(struct razor_error *error)
ali@447
    52
{
ali@447
    53
	if (error->obj_str)
ali@447
    54
		return error->obj_str;
ali@447
    55
ali@447
    56
	if (error->object) {
ali@447
    57
		error->obj_str = razor_concat(error->object, ": ",
ali@447
    58
					      error->str, NULL);
ali@447
    59
		return error->obj_str;
ali@439
    60
	}
ali@439
    61
ali@439
    62
	return error->str;
ali@439
    63
}
ali@439
    64
ali@439
    65
/**
ali@439
    66
 * razor_error_get_primary_text:
ali@439
    67
 *
ali@439
    68
 * Retrieves the basic information about an error. If a summary has been set
ali@439
    69
 * then this will be returned. Otherwise the error string possibly prefixed
ali@447
    70
 * by the object will be returned instead.
ali@439
    71
 *
ali@439
    72
 * Returns: Primary text of error.
ali@439
    73
 **/
ali@439
    74
RAZOR_EXPORT const char *
ali@439
    75
razor_error_get_primary_text(struct razor_error *error)
ali@439
    76
{
ali@439
    77
	if (error->summary)
ali@439
    78
		return error->summary;
ali@439
    79
ali@447
    80
	return razor_error_get_obj_str(error);
ali@439
    81
}
ali@439
    82
ali@439
    83
/**
ali@439
    84
 * razor_error_get_secondary_text:
ali@439
    85
 *
ali@439
    86
 * Retrieves more detailed information about an error, if any. If a summary
ali@447
    87
 * has been set the error string possibly prefixed by the object will be
ali@439
    88
 * returned. Otherwise NULL will be returned.
ali@439
    89
 *
ali@439
    90
 * Returns: Secondary text of error or NULL.
ali@439
    91
 **/
ali@439
    92
RAZOR_EXPORT const char *
ali@439
    93
razor_error_get_secondary_text(struct razor_error *error)
ali@439
    94
{
ali@439
    95
	if (!error->summary)
ali@439
    96
		return NULL;
ali@439
    97
ali@447
    98
	return razor_error_get_obj_str(error);
ali@439
    99
}
ali@439
   100
ali@423
   101
RAZOR_EXPORT const char *
ali@423
   102
razor_error_get_msg(struct razor_error *error)
ali@423
   103
{
ali@439
   104
	const char *primary, *secondary;
ali@423
   105
ali@439
   106
	if (error->msg)
ali@439
   107
		return error->msg;
ali@439
   108
ali@439
   109
	primary = razor_error_get_primary_text(error);
ali@439
   110
ali@439
   111
	secondary = razor_error_get_secondary_text(error);
ali@439
   112
ali@439
   113
	if (secondary) {
ali@439
   114
		error->msg = razor_concat(primary, "\n", secondary, NULL);
ali@439
   115
		return error->msg;
ali@439
   116
	} else
ali@439
   117
		return primary;
ali@423
   118
}
ali@423
   119
ali@423
   120
#ifdef MSWIN_API
ali@423
   121
struct razor_error *
ali@447
   122
razor_error_new_mswin(const wchar_t *object, DWORD err)
ali@423
   123
{
ali@423
   124
	struct razor_error *error;
ali@423
   125
	wchar_t *buf;
ali@423
   126
ali@423
   127
	error = zalloc(sizeof *error);
ali@423
   128
ali@447
   129
	error->domain = RAZOR_MSWIN_ERROR;
ali@447
   130
	error->code = err;
ali@447
   131
ali@447
   132
	if (object)
ali@447
   133
		error->object = razor_utf16_to_utf8(object, -1);
ali@423
   134
ali@455
   135
	switch(err) {
ali@455
   136
	case ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED:
ali@455
   137
		/*
ali@455
   138
		 * Attempting to include files in a transaction on a filesystem
ali@455
   139
		 * that doesn't support them (only NTFS?) produces this error
ali@455
   140
		 * for which the default text isn't very informative. Try and
ali@455
   141
		 * give more useful information.
ali@455
   142
		 */
ali@455
   143
		error->str = strdup("Not allowed (perhaps the filesystem "
ali@455
   144
				    "doesn't support transactions)");
ali@455
   145
		break;
ali@455
   146
	default:
ali@455
   147
		FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
ali@455
   148
			       FORMAT_MESSAGE_FROM_SYSTEM|
ali@455
   149
			       FORMAT_MESSAGE_IGNORE_INSERTS,
ali@455
   150
			       NULL, err,
ali@455
   151
			       MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
ali@455
   152
			       (LPWSTR)&buf, 0, NULL);
ali@455
   153
		error->str = razor_utf16_to_utf8(buf, -1);
ali@455
   154
		LocalFree(buf);
ali@455
   155
		break;
ali@455
   156
	}
ali@423
   157
ali@423
   158
	return error;
ali@423
   159
}
ali@423
   160
ali@423
   161
struct razor_error *
ali@447
   162
razor_error_new_str2(int domain, int code, const wchar_t *object,
ali@447
   163
		     const char *str)
ali@423
   164
{
ali@423
   165
	struct razor_error *error;
ali@423
   166
ali@423
   167
	error = zalloc(sizeof *error);
ali@423
   168
ali@447
   169
	error->domain = domain;
ali@447
   170
	error->code = code;
ali@447
   171
ali@447
   172
	if (object)
ali@447
   173
		error->object = razor_utf16_to_utf8(object, -1);
ali@424
   174
ali@424
   175
	error->str = strdup(str);
ali@424
   176
ali@424
   177
	return error;
ali@424
   178
}
ali@424
   179
#endif	/* MSWIN_API */
ali@424
   180
ali@424
   181
RAZOR_EXPORT struct razor_error *
ali@447
   182
razor_error_new_str(int domain, int code, const char *object, const char *str)
ali@424
   183
{
ali@424
   184
	struct razor_error *error;
ali@424
   185
ali@424
   186
	error = zalloc(sizeof *error);
ali@424
   187
ali@447
   188
	error->domain = domain;
ali@447
   189
	error->code = code;
ali@447
   190
ali@447
   191
	if (object)
ali@447
   192
		error->object = strdup(object);
ali@423
   193
ali@423
   194
	error->str = strdup(str);
ali@423
   195
ali@423
   196
	return error;
ali@423
   197
}
ali@423
   198
ali@439
   199
RAZOR_EXPORT struct razor_error *
ali@439
   200
razor_error_dup(struct razor_error *src, const char *summary)
ali@439
   201
{
ali@439
   202
	struct razor_error *error;
ali@439
   203
ali@439
   204
	error = zalloc(sizeof *error);
ali@439
   205
ali@447
   206
	error->domain = src->domain;
ali@447
   207
	error->code = src->code;
ali@447
   208
ali@439
   209
	if (summary)
ali@439
   210
		error->summary = strdup(summary);
ali@439
   211
	else if (src->summary)
ali@439
   212
		error->summary = strdup(src->summary);
ali@439
   213
ali@447
   214
	if (src->object)
ali@447
   215
		error->object = strdup(src->object);
ali@439
   216
ali@439
   217
	if (src->str)
ali@439
   218
		error->str = strdup(src->str);
ali@439
   219
ali@439
   220
	return error;
ali@439
   221
}
ali@439
   222
ali@424
   223
RAZOR_EXPORT void
ali@424
   224
razor_error_free(struct razor_error *error)
ali@423
   225
{
ali@447
   226
	free(error->object);
ali@424
   227
	free(error->str);
ali@447
   228
	free(error->obj_str);
ali@439
   229
	free(error->summary);
ali@424
   230
	free(error->msg);
ali@424
   231
	free(error);
ali@423
   232
}