Convert GIOError too when converting from GError to AtomError
[cascardo/atompub.git] / src / 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   error->message = g_strdup (message);
71 }
72
73 AtomError *
74 atom_error_new_from_gerror (GError *error)
75 {
76   AtomError *aerr;
77   aerr = atom_error_new ();
78   if (error->domain == G_FILE_ERROR)
79     {
80       switch (error->code)
81         {
82           case G_FILE_ERROR_EXIST:
83           case G_FILE_ERROR_ACCES:
84           case G_FILE_ERROR_PERM:
85             aerr->code = 403;
86             break;
87           case G_FILE_ERROR_NOENT:
88             aerr->code = 404;
89             break;
90           default:
91             aerr->code = 500;
92             break;
93         }
94     }
95   else if (error->domain = G_IO_ERROR)
96     {
97       switch (error->code)
98         {
99           case G_IO_ERROR_NOT_FOUND:
100             aerr->code = 404;
101             break;
102           default:
103             aerr->code = 500;
104             break;
105         }
106     }
107   else
108     {
109       aerr->code = 500;
110     }
111   aerr->message = g_strdup (error->message);
112   return aerr;
113 }