Fixed memory leak when setting error message
[cascardo/atompub.git] / atom / error.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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
20 #include <atompub/atom.h>
21
22 #include <glib.h>
23 #include <gio/gio.h>
24
25 struct _atom_error
26 {
27   int code;
28   char * message;
29 };
30
31 AtomError *
32 atom_error_new ()
33 {
34   AtomError *error;
35   error = g_slice_new (AtomError);
36   error->code = 0;
37   error->message = NULL;
38   return error;
39 }
40
41 void
42 atom_error_delete (AtomError *error)
43 {
44   if (error->message)
45     g_free (error->message);
46   g_slice_free (AtomError, error);
47 }
48
49 int
50 atom_error_code (AtomError *error)
51 {
52   return error->code;
53 }
54
55 void
56 atom_error_code_set (AtomError *error, int code)
57 {
58   error->code = code;
59 }
60
61 char *
62 atom_error_message (AtomError *error)
63 {
64   return error->message;
65 }
66
67 void
68 atom_error_message_set (AtomError *error, char *message)
69 {
70   if (error->message)
71     g_free (error->message);
72   error->message = g_strdup (message);
73 }
74
75 AtomError *
76 atom_error_new_from_gerror (GError *error)
77 {
78   AtomError *aerr;
79   aerr = atom_error_new ();
80   if (error->domain == G_FILE_ERROR)
81     {
82       switch (error->code)
83         {
84           case G_FILE_ERROR_EXIST:
85           case G_FILE_ERROR_ACCES:
86           case G_FILE_ERROR_PERM:
87             aerr->code = 403;
88             break;
89           case G_FILE_ERROR_NOENT:
90             aerr->code = 404;
91             break;
92           default:
93             aerr->code = 500;
94             break;
95         }
96     }
97   else if (error->domain = G_IO_ERROR)
98     {
99       switch (error->code)
100         {
101           case G_IO_ERROR_NOT_FOUND:
102             aerr->code = 404;
103             break;
104           default:
105             aerr->code = 500;
106             break;
107         }
108     }
109   else
110     {
111       aerr->code = 500;
112     }
113   aerr->message = g_strdup (error->message);
114   return aerr;
115 }