cdb3178fc3a66e1e0e424c327588da2f3139ead8
[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
24 struct _atom_error
25 {
26   int code;
27   char * message;
28 };
29
30 AtomError *
31 atom_error_new ()
32 {
33   AtomError *error;
34   error = g_slice_new (AtomError);
35   error->code = 0;
36   error->message = NULL;
37   return error;
38 }
39
40 void
41 atom_error_delete (AtomError *error)
42 {
43   if (error->message)
44     g_free (error->message);
45   g_slice_free (AtomError, error);
46 }
47
48 int
49 atom_error_code (AtomError *error)
50 {
51   return error->code;
52 }
53
54 void
55 atom_error_code_set (AtomError *error, int code)
56 {
57   error->code = code;
58 }
59
60 char *
61 atom_error_message (AtomError *error)
62 {
63   return error->message;
64 }
65
66 void
67 atom_error_message_set (AtomError *error, char *message)
68 {
69   error->message = g_strdup (message);
70 }
71
72 AtomError *
73 atom_error_new_from_gerror (GError *error)
74 {
75   AtomError *aerr;
76   aerr = atom_error_new ();
77   if (error->domain == G_FILE_ERROR)
78     {
79       switch (error->code)
80         {
81           case G_FILE_ERROR_EXIST:
82           case G_FILE_ERROR_ACCES:
83           case G_FILE_ERROR_PERM:
84             aerr->code = 403;
85             break;
86           case G_FILE_ERROR_NOENT:
87             aerr->code = 404;
88             break;
89           default:
90             aerr->code = 500;
91             break;
92         }
93     }
94   else
95     {
96       aerr->code = 500;
97     }
98   aerr->message = g_strdup (error->message);
99   return aerr;
100 }