Code

frontend: Unified error reporting to the client.
authorSebastian Harl <sh@tokkee.org>
Thu, 28 Nov 2013 17:28:26 +0000 (18:28 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 28 Nov 2013 17:28:26 +0000 (18:28 +0100)
Keep track of the latest error in a connection object and report that back to
the client in case a command handler failed.

src/frontend/connection.c
src/frontend/query.c
src/include/frontend/connection-private.h

index 21bca4f28e1046a570c202f03eed89ed019e17a9..962cd9cf8d9a14f9a3bc2f2a864e993958eb467d 100644 (file)
@@ -65,6 +65,12 @@ connection_init(sdb_object_t *obj, va_list ap)
                                "for a new connection");
                return -1;
        }
+       conn->errbuf = sdb_strbuf_create(0);
+       if (! conn->errbuf) {
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate an error buffer "
+                               "for a new connection");
+               return -1;
+       }
 
        conn->client_addr_len = sizeof(conn->client_addr);
        conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
@@ -128,6 +134,8 @@ connection_destroy(sdb_object_t *obj)
 
        sdb_strbuf_destroy(conn->buf);
        conn->buf = NULL;
+       sdb_strbuf_destroy(conn->errbuf);
+       conn->errbuf = NULL;
 } /* connection_destroy */
 
 static sdb_type_t connection_type = {
@@ -164,6 +172,9 @@ command_handle(sdb_conn_t *conn)
        sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)",
                        conn->cmd, conn->cmd_len);
 
+       /* reset */
+       sdb_strbuf_sprintf(conn->errbuf, "");
+
        switch (conn->cmd) {
                case CONNECTION_PING:
                        status = sdb_connection_ping(conn);
@@ -178,16 +189,18 @@ command_handle(sdb_conn_t *conn)
 
                default:
                {
-                       char errbuf[1024];
                        sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command");
-                       snprintf(errbuf, sizeof(errbuf), "Invalid command %#x", conn->cmd);
-                       sdb_connection_send(conn, CONNECTION_ERROR,
-                                       (uint32_t)(strlen(errbuf) + 1), errbuf);
+                       sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
                        status = -1;
                        break;
                }
        }
 
+       if (status)
+               sdb_connection_send(conn, CONNECTION_ERROR,
+                               (uint32_t)sdb_strbuf_len(conn->errbuf),
+                               sdb_strbuf_string(conn->errbuf));
+
        /* remove the command from the buffer */
        if (conn->cmd_len)
                sdb_strbuf_skip(conn->buf, conn->cmd_len);
index 9e3b66a3a5c2b55579dccb5fdc4741631930e95d..fa493a6dd04be5a8139c0cae0485d2bea43703c6 100644 (file)
@@ -50,8 +50,7 @@ sdb_fe_list(sdb_conn_t *conn)
                                "buffer to handle LIST command: %s",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
 
-               /* XXX: send error message */
-               sdb_connection_send(conn, CONNECTION_ERROR, 0, NULL);
+               sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
                sdb_strbuf_destroy(buf);
                return -1;
        }
@@ -59,7 +58,7 @@ sdb_fe_list(sdb_conn_t *conn)
        if (sdb_store_tojson(buf)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
                                "store to JSON");
-               sdb_connection_send(conn, CONNECTION_ERROR, 0, NULL);
+               sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
                sdb_strbuf_destroy(buf);
                return -1;
        }
index d484a0863e8a670e8a8a69a9679c008f8b58b418..6cf7395b7c122b0c5c329c622be47745f71f5bb3 100644 (file)
@@ -60,6 +60,8 @@ struct sdb_conn {
        uint32_t cmd;
        uint32_t cmd_len;
 
+       sdb_strbuf_t *errbuf;
+
        /* user information */
        char *username; /* NULL if the user has not been authenticated */
 };