aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-02 11:14:44 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-02 11:14:44 -0600
commit9c25735888a47dbba8ee536836b5c0a3dd75f627 (patch)
tree22a5b4ece50da9af4314c3f59d0941a8a9c02b57 /src/clog.c
parent9206b6537b93db8b2d8e31f26de4da8a5c8ac633 (diff)
Clean up error handling
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c82
1 files changed, 51 insertions, 31 deletions
diff --git a/src/clog.c b/src/clog.c
index 8dd9b9f..3ef2121 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -47,8 +47,11 @@ static int render_md(const char *, struct kore_buf *);
static const char *accept_json = "application/json";
static const char *database = "db";
-static const char *req_err_generic = "There was an error processing the request.";
-static const char *req_err_no_resource = "There was an error processing the request.";
+
+static const char * const error_msg[] = {
+ [HTTP_STATUS_NOT_FOUND] = "Resource not found.",
+ [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.",
+};
void post_request_init(struct post_request *post_req)
{
@@ -117,17 +120,39 @@ render_posts(struct http_request *req, const char *resource)
{
post_req.type = 0;
}
+ else
+ {
+ post_req.type = 1;
+ }
}
if (post_req.type == 0)
{
http_response_header(post_req.req, "content-type", "application/json; charset=utf-8");
- err = render_posts_json(&post_req);
+ (void) render_posts_json(&post_req);
+ if (post_req.resp_status != HTTP_STATUS_OK)
+ {
+ kore_buf_appendf(
+ post_req.resp_buf,
+ (const char *) asset_error_json,
+ error_msg[post_req.resp_status]
+ );
+ }
}
else
{
http_response_header(post_req.req, "content-type", "text/html; charset=utf-8");
- err = render_posts_html(&post_req);
+ kore_buf_append(post_req.resp_buf, asset_index_begin_html, asset_len_index_begin_html);
+ (void) render_posts_html(&post_req);
+ if (post_req.resp_status != HTTP_STATUS_OK)
+ {
+ kore_buf_appendf(
+ post_req.resp_buf,
+ (const char *) asset_error_html,
+ error_msg[post_req.resp_status]
+ );
+ }
+ kore_buf_append(post_req.resp_buf, asset_index_end_html, asset_len_index_end_html);
}
http_response(
@@ -139,7 +164,7 @@ render_posts(struct http_request *req, const char *resource)
post_request_cleanup(&post_req);
- return err;
+ return KORE_RESULT_OK;
}
int
@@ -158,8 +183,6 @@ render_posts_html(struct post_request *post_req)
kore_pgsql_init(&sql);
- kore_buf_append(post_req->resp_buf, asset_index_begin_html, asset_len_index_begin_html);
-
// Initialize our kore_pgsql data structure with the database name
// we want to connect to (note that we registered this earlier with
// kore_pgsql_register()). We also say we will perform a synchronous
@@ -168,11 +191,6 @@ render_posts_html(struct post_request *post_req)
if (err == KORE_RESULT_ERROR)
{
post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR;
- kore_buf_appendf(
- post_req->resp_buf,
- (const char *) asset_error_html,
- req_err_generic
- );
kore_pgsql_logerror(&sql);
goto out;
}
@@ -205,11 +223,6 @@ render_posts_html(struct post_request *post_req)
if (err == KORE_RESULT_ERROR)
{
post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR;
- kore_buf_appendf(
- post_req->resp_buf,
- (const char *) asset_error_html,
- req_err_generic
- );
kore_pgsql_logerror(&sql);
goto out;
}
@@ -217,7 +230,11 @@ render_posts_html(struct post_request *post_req)
// Iterate over posts and render them.
rows = kore_pgsql_ntuples(&sql);
- // if (rows == 0) ...
+ if (rows == 0)
+ {
+ post_req->resp_status = HTTP_STATUS_NOT_FOUND;
+ goto out;
+ }
for (row = 0; row < rows; row++)
{
@@ -252,8 +269,6 @@ render_posts_html(struct post_request *post_req)
out: ;
- kore_buf_append(post_req->resp_buf, asset_index_end_html, asset_len_index_end_html);
-
kore_pgsql_cleanup(&sql);
return KORE_RESULT_OK;
@@ -276,11 +291,6 @@ render_posts_json(struct post_request *post_req)
if (err == KORE_RESULT_ERROR)
{
post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR;
- kore_buf_appendf(
- post_req->resp_buf,
- (const char *) asset_error_json,
- req_err_generic
- );
kore_pgsql_logerror(&sql);
goto out;
}
@@ -313,24 +323,34 @@ render_posts_json(struct post_request *post_req)
if (err == KORE_RESULT_ERROR)
{
post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR;
- kore_buf_appendf(
- post_req->resp_buf,
- (const char *) asset_error_json,
- req_err_generic
- );
kore_pgsql_logerror(&sql);
goto out;
}
rows = kore_pgsql_ntuples(&sql);
+ kore_log(LOG_DEBUG, "JSON rows: %d", rows);
+
+ int fields = kore_pgsql_nfields(&sql);
+ kore_log(LOG_DEBUG, "JSON fields: %d", fields);
+
+ int len = kore_pgsql_getlength(&sql, 0, 0);
+ kore_log(LOG_DEBUG, "JSON fields: %d", fields);
+
+ kore_pgsql_logerror(&sql);
+
if (rows == 1) {
json = kore_pgsql_getvalue(&sql, 0, 0);
+ kore_log(LOG_DEBUG, "JSON data: %s", json);
+
+ len = kore_pgsql_getlength(&sql, 0, 0);
+ kore_log(LOG_DEBUG, "JSON data len: %d", len);
+
kore_buf_append(
post_req->resp_buf,
json,
strlen(json)
);
- } // else {
+ }
out: ;