New error interface: migrated core, backends and frontends to it
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Sun, 3 Aug 2008 21:15:54 +0000 (18:15 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Sun, 3 Aug 2008 21:15:54 +0000 (18:15 -0300)
14 files changed:
backend/files/giochannel.c
backend/gio/gio.c
config/gkeyfile.c
frontend/cgi/cgi.c
include/atompub/Makefile.am
include/atompub/atom-glib.h [new file with mode: 0644]
include/atompub/entry.h
include/atompub/error-glib.h [new file with mode: 0644]
include/atompub/error.h
src/Makefile.am
src/backend.c
src/ctx.c
src/error.c [new file with mode: 0644]
src/main.c

index e8a4bb7..dde0c56 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <glib.h>
 #include <atompub/atom.h>
+#include <atompub/atom-glib.h>
 
 static gchar *
 giochannel_iri_to_filename (AtomCtx *ctx, IRI *iri)
@@ -44,15 +45,19 @@ giochannel_atom_retrieve_resource (AtomCtx *ctx, IRI *iri)
   g_free (filename);
   if (channel == NULL)
     {
-      atom_error_set (ctx, error);
+      AtomError *aerr = atom_error_new_from_gerror (error);
+      atom_error_set (ctx, aerr);
+      g_error_free (error);
       return NULL;
     }
   error = NULL;
   if (g_io_channel_read_to_end (channel, &data, &len, &error) !=
       G_IO_STATUS_NORMAL)
     {
+      AtomError *aerr = atom_error_new_from_gerror (error);
       g_io_channel_unref (channel);
-      atom_error_set (ctx, error);
+      atom_error_set (ctx, aerr);
+      g_error_free (error);
       return NULL;
     }
   g_io_channel_unref (channel);
index 9a7244a..b930e10 100644 (file)
@@ -20,6 +20,7 @@
 #include <glib.h>
 #include <gio/gio.h>
 #include <atompub/atom.h>
+#include <atompub/atom-glib.h>
 
 static GFile *
 gio_iri_to_file (AtomCtx *ctx, IRI *iri)
@@ -45,8 +46,10 @@ gio_atom_retrieve_resource (AtomCtx *ctx, IRI *iri)
   error = NULL;
   if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
     {
+      AtomError *aerr = atom_error_new_from_gerror (error);
       g_object_unref (file);
-      atom_error_set (ctx, error);
+      atom_error_set (ctx, aerr);
+      g_error_free (error);
       return NULL;
     }
   g_object_unref (file);
index 33e54e7..891f4b5 100644 (file)
@@ -30,7 +30,11 @@ gkeyfile_atom_config_get_str (AtomCtx *ctx, gchar *group, gchar * key)
   value = g_key_file_get_string (keyfile, group, key, &error);
   if (value == NULL)
     {
-      atom_error_set (ctx, error);
+      AtomError *aerr = atom_error_new ();
+      atom_error_code_set (aerr, 500);
+      atom_error_message_set (aerr, error->message);
+      atom_error_set (ctx, aerr);
+      g_error_free (error);
     }
   return value;
 }
@@ -44,7 +48,11 @@ gkeyfile_atom_config_init (AtomCtx *ctx)
   if (!g_key_file_load_from_file (keyfile, "atompub.conf",
                                  G_KEY_FILE_NONE, &error))
     {
-      atom_error_set (ctx, error);
+      AtomError *aerr = atom_error_new ();
+      atom_error_code_set (aerr, 500);
+      atom_error_message_set (aerr, error->message);
+      atom_error_set (ctx, aerr);
+      g_error_free (error);
       return;
     }
   atom_config_data_set (ctx, keyfile);
index 472d3ec..f61ea67 100644 (file)
@@ -39,7 +39,7 @@ cgi_serve_request (AtomCtx *ctx)
     {
       IRI *iri = iri_new ();
       Atom *atom;
-      GError *error;
+      AtomError *error;
       iri_set_path (iri, path);
       atom = atom_retrieve_resource (ctx, iri);
       iri_delete (iri);
@@ -52,19 +52,13 @@ cgi_serve_request (AtomCtx *ctx)
        }
       else if ((error = atom_error_get (ctx)) != NULL)
        {
-         if (error->domain == G_FILE_ERROR &&
-             (error->code == G_FILE_ERROR_EXIST ||
-              error->code == G_FILE_ERROR_ACCES ||
-              error->code == G_FILE_ERROR_PERM))
-           fprintf (stdout, "Status: 403 %s\n\n%s\n", error->message,
-                    error->message);
-         else if (error->domain == G_FILE_ERROR &&
-                  error->code == G_FILE_ERROR_NOENT)
-           fprintf (stdout, "Status: 404 %s\n\n%s\n", error->message,
-                    error->message);
-         else
-           fprintf (stdout, "Status: 500 %s\n\n%s\n", error->message,
-                    error->message);
+         int code = atom_error_code (error);
+         char *message = atom_error_message (error);
+         fprintf (stdout, "Status: %d %s\n\n%s\n", code, message, message);
+       }
+      else
+        {
+         fprintf (stdout, "Status: 500 Server error\n\nServer error\n");
        }
     }
   else
index d760aed..6926c5d 100644 (file)
@@ -1 +1,2 @@
-include_HEADERS = atom.h config.h ctx.h entry.h error.h feed.h iri.h backend.h
+include_HEADERS = atom.h config.h ctx.h entry.h error.h feed.h \
+               iri.h backend.h atom-glib.h error-glib.h
diff --git a/include/atompub/atom-glib.h b/include/atompub/atom-glib.h
new file mode 100644 (file)
index 0000000..107aeaa
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  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.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#ifndef ATOMPUB_ATOM_GLIB_H
+#define ATOMPUB_ATOM_GLIB_H
+
+#include <atompub/error-glib.h>
+
+#endif
index 0bf4253..dd2cf05 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef ATOMPUB_ENTRY_H
 #define ATOMPUB_ENTRY_H
 
-#include <atompub/ctx.h>
+#include <sys/types.h>
 
 typedef struct _atom_entry Atom;
 
diff --git a/include/atompub/error-glib.h b/include/atompub/error-glib.h
new file mode 100644 (file)
index 0000000..68d748a
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  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.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#ifndef ATOMPUB_ERROR_GLIB_H
+#define ATOMPUB_ERROR_GLIB_H
+
+#include <atompub/error.h>
+#include <glib.h>
+
+AtomError * atom_error_new_from_gerror (GError *);
+
+#endif
index 48bb429..9d7cae5 100644 (file)
 #define ATOMPUB_ERROR_H
 
 #include <atompub/ctx.h>
-#include <glib.h>
 
-GError *atom_error_get (AtomCtx *);
-void atom_error_set (AtomCtx *, GError *);
+typedef struct _atom_error AtomError;
+
+AtomError * atom_error_get (AtomCtx *);
+void atom_error_set (AtomCtx *, AtomError *);
+AtomError * atom_error_new (void);
+void atom_error_delete (AtomError *);
+int atom_error_code (AtomError *);
+void atom_error_code_set (AtomError *, int);
+char * atom_error_message (AtomError *);
+void atom_error_message_set (AtomError *, char *);
 
 #endif
index 4918e79..069bffa 100644 (file)
@@ -1,5 +1,5 @@
 bin_PROGRAMS = atompub
-atompub_SOURCES = main.c config.c ctx.c backend.c
+atompub_SOURCES = main.c config.c ctx.c backend.c error.c
 atompub_CFLAGS = -I$(top_srcdir)/include
 atompub_CFLAGS += $(GLIB_CFLAGS)
 atompub_LDADD = -L$(top_builddir)/backend/files -lgiochannel
index ded8229..f8bcf35 100644 (file)
@@ -18,6 +18,7 @@
 
 
 #include <atompub/atom.h>
+#include <glib.h>
 
 struct _atom_backend
 {
index 73f17fc..e9d940b 100644 (file)
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -23,7 +23,7 @@
 
 struct _atom_ctx
 {
-  GError *error;
+  AtomError *error;
   gpointer config_data;
   AtomBackend *backend;
 };
@@ -43,21 +43,21 @@ void
 atom_ctx_delete (AtomCtx *ctx)
 {
   if (ctx->error)
-    g_error_free (ctx->error);
+    atom_error_delete (ctx->error);
   if (ctx->backend)
     atom_backend_delete (ctx->backend);
   g_slice_free (AtomCtx, ctx);
 }
 
 void
-atom_error_set (AtomCtx *ctx, GError *error)
+atom_error_set (AtomCtx *ctx, AtomError *error)
 {
   if (ctx->error)
-    g_error_free (ctx->error);
+    atom_error_delete (ctx->error);
   ctx->error = error;
 }
 
-GError *
+AtomError *
 atom_error_get (AtomCtx *ctx)
 {
   return ctx->error;
diff --git a/src/error.c b/src/error.c
new file mode 100644 (file)
index 0000000..d272cb2
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  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.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <atompub/atom.h>
+
+#include <glib.h>
+
+struct _atom_error
+{
+  int code;
+  char * message;
+};
+
+AtomError *
+atom_error_new ()
+{
+  AtomError *error;
+  error = g_slice_new (AtomError);
+  error->code = 0;
+  error->message = NULL;
+  return error;
+}
+
+void
+atom_error_delete (AtomError *error)
+{
+  if (error->message)
+    g_free (error->message);
+  g_slice_free (AtomError, error);
+}
+
+int
+atom_error_code (AtomError *error)
+{
+  return error->code;
+}
+
+void
+atom_error_code_set (AtomError *error, int code)
+{
+  error->code = code;
+}
+
+char *
+atom_error_message (AtomError *error)
+{
+  return error->message;
+}
+
+void
+atom_error_message_set (AtomError *error, char *message)
+{
+  error->message = g_strdup (message);
+}
+
+AtomError *
+atom_error_new_from_gerror (GError *error)
+{
+  AtomError *aerr;
+  aerr = atom_error_new ();
+  if (error->domain == G_FILE_ERROR)
+    {
+      switch (error->code)
+       {
+         case G_FILE_ERROR_EXIST:
+         case G_FILE_ERROR_ACCES:
+         case G_FILE_ERROR_PERM:
+           aerr->code = 403;
+           break;
+         case G_FILE_ERROR_NOENT:
+           aerr->code = 404;
+           break;
+         default:
+           aerr->code = 500;
+           break;
+       }
+    }
+  else
+    {
+      aerr->code = 500;
+    }
+  aerr->message = error->message;
+  return aerr;
+}
index 2a790d0..4c10bd1 100644 (file)
@@ -32,14 +32,14 @@ main (int argc, char **argv)
   AtomCtx *ctx;
   IRI *iri;
   Atom *atom;
-  GError *error;
+  AtomError *error;
   g_type_init ();
   ctx = atom_ctx_new ();
   atom_config_init (ctx);
   atom_backend_set (ctx, giochannel_backend ());
   if ((error = atom_error_get (ctx)) != NULL)
     {
-      g_message ("%s\n", error->message);
+      g_message ("%s\n", atom_error_message (error));
       exit (1);
     }
   cgi_serve_request (ctx);