summaryrefslogtreecommitdiff
path: root/src/yelp-error.c
blob: 63bd77481baf6a5d0e734d4d395f93cf93a3d9f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2002 Mikael Hallendal <micke@imendio.com>
 * Copyright (C) 2007 Shaun McCance  <shaunm@gnome.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <glib.h>
#include <glib/gi18n.h>

#include "yelp-error.h"

struct _YelpError {
    gchar *title;
    gchar *message;
};

static YelpError *
yelp_error_new_valist (gchar *title, gchar *format, va_list args)
{
    YelpError *error;

    error = g_slice_new (YelpError);
    error->title = g_strdup (title);
    error->message = g_strdup_vprintf (format, args);

    return error;
}

YelpError *
yelp_error_new (gchar *title, gchar *format, ...)
{
    YelpError *error;
    va_list args;

    va_start (args, format);
    error = yelp_error_new_valist (title, format, args);
    va_end (args);

    return error;
}

void
yelp_error_set (YelpError **error, gchar *title, gchar *format, ...)
{
    YelpError *new;
    va_list args;

    va_start (args, format);
    new = yelp_error_new_valist (title, format, args);
    va_end (args);

    if (*error == NULL)
	*error = new;
    else
	g_warning
	    ("YelpError set over the top of a previous YelpError or uninitialized\n"
	     "memory. This indicates a bug in someone's code. You must ensure an\n"
	     "error is NULL before it's set. The overwriting error message was:\n"
	     "%s",
	     new->message);
}

YelpError *
yelp_error_copy (YelpError *error)
{
    YelpError *new;

    new = g_slice_new (YelpError);
    new->title = g_strdup (error->title);
    new->message = g_strdup (error->message);

    return new;
}

const gchar *
yelp_error_get_title (YelpError *error)
{
    g_return_val_if_fail (error != NULL, NULL);
    return error->title;
}

const gchar *
yelp_error_get_message (YelpError *error)
{
    g_return_val_if_fail (error != NULL, NULL);
    return error->message;
}

void
yelp_error_free (YelpError *error)
{
    g_return_if_fail (error != NULL);
    g_free (error->title);
    g_free (error->message);
    g_slice_free (YelpError, error);
}

/******************************************************************************/

GQuark
yelp_gerror_quark (void)
{
    static GQuark q = 0;

    if (q == 0)
	q = g_quark_from_static_string ("yelp-error-quark");

    return q;
}

const gchar *
yelp_gerror_get_title (GError *error)
{
    if (!error || error->domain != YELP_GERROR)
	return _("Unknown Error");

    switch (error->code) {
    case YELP_GERROR_IO:
	return _("Could Not Read File");
    }

    return _("Unknown Error");
}

const gchar *
yelp_gerror_get_message (GError *error)
{
    if (!error || !error->message)
	return _("No information is available about this error.");
    else
	return error->message;
}