From a275f9636cdb8c70aef3c35c59b70b843bb4a38b Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sat, 20 Feb 2016 00:01:54 +0100 Subject: ZyklonB: revisit error handling --- zyklonb.c | 72 ++++++++++++++++----------------------------------------------- 1 file changed, 18 insertions(+), 54 deletions(-) diff --git a/zyklonb.c b/zyklonb.c index cd40254..c74af84 100644 --- a/zyklonb.c +++ b/zyklonb.c @@ -310,8 +310,7 @@ irc_get_boolean_from_config if (set_boolean_if_valid (value, str)) return true; - error_set (e, "invalid configuration value for `%s'", name); - return false; + FAIL ("invalid configuration value for `%s'", name); } static bool @@ -443,8 +442,7 @@ error_ssl_1: // multiple errors on the OpenSSL stack. if (!error_info) error_info = ERR_error_string (ERR_get_error (), NULL); - error_set (e, "%s: %s", "could not initialize TLS", error_info); - return false; + FAIL ("%s: %s", "could not initialize TLS", error_info); } static bool @@ -457,11 +455,8 @@ irc_establish_connection (struct bot_context *ctx, int err = getaddrinfo (host, port, &gai_hints, &gai_result); if (err) - { - error_set (e, "%s: %s: %s", - "connection failed", "getaddrinfo", gai_strerror (err)); - return false; - } + FAIL ("%s: %s: %s", "connection failed", + "getaddrinfo", gai_strerror (err)); int sockfd; for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next) @@ -502,10 +497,7 @@ irc_establish_connection (struct bot_context *ctx, freeaddrinfo (gai_result); if (!gai_iter) - { - error_set (e, "connection failed"); - return false; - } + FAIL ("connection failed"); ctx->irc_fd = sockfd; return true; @@ -1034,37 +1026,21 @@ plugin_load (struct bot_context *ctx, const char *name, struct error **e) { const char *plugin_dir = str_map_find (&ctx->config, "plugin_dir"); if (!plugin_dir) - { - error_set (e, "plugin directory not set"); - return false; - } - + FAIL ("plugin directory not set"); if (!is_valid_plugin_name (name)) - { - error_set (e, "invalid plugin name"); - return false; - } - + FAIL ("invalid plugin name"); if (str_map_find (&ctx->plugins_by_name, name)) - { - error_set (e, "the plugin has already been loaded"); - return false; - } + FAIL ("the plugin has already been loaded"); int stdin_pipe[2]; if (pipe (stdin_pipe) == -1) - { - error_set (e, "%s: %s: %s", - "failed to load the plugin", "pipe", strerror (errno)); - goto fail_1; - } + FAIL ("%s: %s", "pipe", strerror (errno)); int stdout_pipe[2]; if (pipe (stdout_pipe) == -1) { - error_set (e, "%s: %s: %s", - "failed to load the plugin", "pipe", strerror (errno)); - goto fail_2; + error_set (e, "%s: %s", "pipe", strerror (errno)); + goto fail_1; } set_cloexec (stdin_pipe[1]); @@ -1073,9 +1049,8 @@ plugin_load (struct bot_context *ctx, const char *name, struct error **e) pid_t pid = fork (); if (pid == -1) { - error_set (e, "%s: %s: %s", - "failed to load the plugin", "fork", strerror (errno)); - goto fail_3; + error_set (e, "%s: %s", "fork", strerror (errno)); + goto fail_2; } if (pid == 0) @@ -1133,13 +1108,12 @@ plugin_load (struct bot_context *ctx, const char *name, struct error **e) poller_fd_set (&plugin->read_event, POLLIN); return true; -fail_3: +fail_2: xclose (stdout_pipe[0]); xclose (stdout_pipe[1]); -fail_2: +fail_1: xclose (stdin_pipe[0]); xclose (stdin_pipe[1]); -fail_1: return false; } @@ -1149,10 +1123,7 @@ plugin_unload (struct bot_context *ctx, const char *name, struct error **e) struct plugin *plugin = str_map_find (&ctx->plugins_by_name, name); if (!plugin) - { - error_set (e, "no such plugin is loaded"); - return false; - } + FAIL ("no such plugin is loaded"); plugin_zombify (plugin); @@ -1781,10 +1752,7 @@ irc_connect (struct bot_context *ctx, struct error **e) // TODO: again, get rid of `struct error' in here. The question is: how // do we tell our caller that he should not try to reconnect? if (!irc_host) - { - error_set (e, "no hostname specified in configuration"); - return false; - } + FAIL ("no hostname specified in configuration"); bool use_tls; if (!irc_get_boolean_from_config (ctx, "tls", &use_tls, e)) @@ -1829,11 +1797,7 @@ parse_config (struct bot_context *ctx, struct error **e) const char *delay_str = str_map_find (&ctx->config, "reconnect_delay"); hard_assert (delay_str != NULL); // We have a default value for this if (!xstrtoul (&ctx->reconnect_delay, delay_str, 10)) - { - error_set (e, "invalid configuration value for `%s'", - "reconnect_delay"); - return false; - } + FAIL ("invalid configuration value for `%s'", "reconnect_delay"); hard_assert (!ctx->admin_re); const char *admin = str_map_find (&ctx->config, "admin"); -- cgit v1.2.3-70-g09d2