ZyklonB: revisit error handling

This commit is contained in:
Přemysl Eric Janouch 2016-02-20 00:01:54 +01:00
parent 056e0a4765
commit a275f9636c
1 changed files with 18 additions and 54 deletions

View File

@ -310,8 +310,7 @@ irc_get_boolean_from_config
if (set_boolean_if_valid (value, str)) if (set_boolean_if_valid (value, str))
return true; return true;
error_set (e, "invalid configuration value for `%s'", name); FAIL ("invalid configuration value for `%s'", name);
return false;
} }
static bool static bool
@ -443,8 +442,7 @@ error_ssl_1:
// multiple errors on the OpenSSL stack. // multiple errors on the OpenSSL stack.
if (!error_info) if (!error_info)
error_info = ERR_error_string (ERR_get_error (), NULL); error_info = ERR_error_string (ERR_get_error (), NULL);
error_set (e, "%s: %s", "could not initialize TLS", error_info); FAIL ("%s: %s", "could not initialize TLS", error_info);
return false;
} }
static bool static bool
@ -457,11 +455,8 @@ irc_establish_connection (struct bot_context *ctx,
int err = getaddrinfo (host, port, &gai_hints, &gai_result); int err = getaddrinfo (host, port, &gai_hints, &gai_result);
if (err) if (err)
{ FAIL ("%s: %s: %s", "connection failed",
error_set (e, "%s: %s: %s", "getaddrinfo", gai_strerror (err));
"connection failed", "getaddrinfo", gai_strerror (err));
return false;
}
int sockfd; int sockfd;
for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next) 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); freeaddrinfo (gai_result);
if (!gai_iter) if (!gai_iter)
{ FAIL ("connection failed");
error_set (e, "connection failed");
return false;
}
ctx->irc_fd = sockfd; ctx->irc_fd = sockfd;
return true; 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"); const char *plugin_dir = str_map_find (&ctx->config, "plugin_dir");
if (!plugin_dir) if (!plugin_dir)
{ FAIL ("plugin directory not set");
error_set (e, "plugin directory not set");
return false;
}
if (!is_valid_plugin_name (name)) if (!is_valid_plugin_name (name))
{ FAIL ("invalid plugin name");
error_set (e, "invalid plugin name");
return false;
}
if (str_map_find (&ctx->plugins_by_name, name)) if (str_map_find (&ctx->plugins_by_name, name))
{ FAIL ("the plugin has already been loaded");
error_set (e, "the plugin has already been loaded");
return false;
}
int stdin_pipe[2]; int stdin_pipe[2];
if (pipe (stdin_pipe) == -1) if (pipe (stdin_pipe) == -1)
{ FAIL ("%s: %s", "pipe", strerror (errno));
error_set (e, "%s: %s: %s",
"failed to load the plugin", "pipe", strerror (errno));
goto fail_1;
}
int stdout_pipe[2]; int stdout_pipe[2];
if (pipe (stdout_pipe) == -1) if (pipe (stdout_pipe) == -1)
{ {
error_set (e, "%s: %s: %s", error_set (e, "%s: %s", "pipe", strerror (errno));
"failed to load the plugin", "pipe", strerror (errno)); goto fail_1;
goto fail_2;
} }
set_cloexec (stdin_pipe[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 (); pid_t pid = fork ();
if (pid == -1) if (pid == -1)
{ {
error_set (e, "%s: %s: %s", error_set (e, "%s: %s", "fork", strerror (errno));
"failed to load the plugin", "fork", strerror (errno)); goto fail_2;
goto fail_3;
} }
if (pid == 0) 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); poller_fd_set (&plugin->read_event, POLLIN);
return true; return true;
fail_3: fail_2:
xclose (stdout_pipe[0]); xclose (stdout_pipe[0]);
xclose (stdout_pipe[1]); xclose (stdout_pipe[1]);
fail_2: fail_1:
xclose (stdin_pipe[0]); xclose (stdin_pipe[0]);
xclose (stdin_pipe[1]); xclose (stdin_pipe[1]);
fail_1:
return false; 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); struct plugin *plugin = str_map_find (&ctx->plugins_by_name, name);
if (!plugin) if (!plugin)
{ FAIL ("no such plugin is loaded");
error_set (e, "no such plugin is loaded");
return false;
}
plugin_zombify (plugin); 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 // 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? // do we tell our caller that he should not try to reconnect?
if (!irc_host) if (!irc_host)
{ FAIL ("no hostname specified in configuration");
error_set (e, "no hostname specified in configuration");
return false;
}
bool use_tls; bool use_tls;
if (!irc_get_boolean_from_config (ctx, "tls", &use_tls, e)) 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"); const char *delay_str = str_map_find (&ctx->config, "reconnect_delay");
hard_assert (delay_str != NULL); // We have a default value for this hard_assert (delay_str != NULL); // We have a default value for this
if (!xstrtoul (&ctx->reconnect_delay, delay_str, 10)) if (!xstrtoul (&ctx->reconnect_delay, delay_str, 10))
{ FAIL ("invalid configuration value for `%s'", "reconnect_delay");
error_set (e, "invalid configuration value for `%s'",
"reconnect_delay");
return false;
}
hard_assert (!ctx->admin_re); hard_assert (!ctx->admin_re);
const char *admin = str_map_find (&ctx->config, "admin"); const char *admin = str_map_find (&ctx->config, "admin");