diff --git a/src/lib-sieve/sieve-error.c b/src/lib-sieve/sieve-error.c
index bc7aa443cccc06d04a5e752c126bbabae0959ce2..a3c23f1e1e68b17328c65be57fce0cbe13406c89 100644
--- a/src/lib-sieve/sieve-error.c
+++ b/src/lib-sieve/sieve-error.c
@@ -1300,13 +1300,22 @@ static const char *ATTR_FORMAT(3, 0) _expand_message
 	struct var_expand_table *table =
 		array_get_modifiable(&ehandler->table, &count);
 	string_t *str = t_str_new(256);
+	const char *error;
+	static bool expand_error_logged = FALSE;
 
 	/* Fill in substitution items */
 	table[0].value = t_strdup_vprintf(fmt, args);
 	table[1].value = location;
 
 	/* Expand variables */
-	var_expand(str, ehandler->format, table);
+	if (var_expand(str, ehandler->format, table, &error) <= 0 &&
+	    !expand_error_logged) {
+		/* Log the error only once. This also prevents recursively
+		   looping back here. */
+		expand_error_logged = TRUE;
+		sieve_sys_error(_ehandler->svinst,
+			"Failed to expand error message: %s", error);
+	}
 
 	return str_c(str);
 }
diff --git a/src/managesieve/main.c b/src/managesieve/main.c
index c2ae6c113bebf1f86981b2ad0641da855c078cf8..6bf5e3006d9a3ad583cb3c1339c70f89acdde09e 100644
--- a/src/managesieve/main.c
+++ b/src/managesieve/main.c
@@ -129,6 +129,7 @@ client_create_from_input(const struct mail_storage_service_input *input,
 	struct mail_user *mail_user;
 	struct client *client;
 	struct managesieve_settings *set;
+	const char *error;
 
 	if (mail_storage_service_lookup_next(storage_service, input,
 					     &user, &mail_user, error_r) <= 0)
@@ -139,8 +140,13 @@ client_create_from_input(const struct mail_storage_service_input *input,
 	if (set->verbose_proctitle)
 		verbose_proctitle = TRUE;
 
-	settings_var_expand(&managesieve_setting_parser_info, set, mail_user->pool,
-			    mail_user_var_expand_table(mail_user));
+	if (settings_var_expand(&managesieve_setting_parser_info, set, mail_user->pool,
+				mail_user_var_expand_table(mail_user), &error) <= 0) {
+		i_error("Failed to expand settings: %s", error);
+		mail_storage_service_user_free(&user);
+		mail_user_unref(&mail_user);
+		return -1;
+	}
 
 	client = client_create
 		(fd_in, fd_out, input->session_id, mail_user, user, set);
diff --git a/src/managesieve/managesieve-client.c b/src/managesieve/managesieve-client.c
index cad6ef4bbbf2b6dd98b1f0384b58440771d1802f..48a33aaf625db05adacb8b2192b3f035215ea0ec 100644
--- a/src/managesieve/managesieve-client.c
+++ b/src/managesieve/managesieve-client.c
@@ -206,6 +206,7 @@ static const char *client_stats(struct client *client)
 	};
 	struct var_expand_table *tab;
 	string_t *str;
+	const char *error;
 
 	tab = t_malloc_no0(sizeof(static_tab));
 	memcpy(tab, static_tab, sizeof(static_tab));
@@ -223,7 +224,10 @@ static const char *client_stats(struct client *client)
 	tab[10].value = client->session_id;
 
 	str = t_str_new(128);
-	var_expand(str, client->set->managesieve_logout_format, tab);
+	if (var_expand(str, client->set->managesieve_logout_format, tab, &error) <= 0) {
+		i_error("Failed to expand managesieve_logout_format=%s: %s",
+			client->set->managesieve_logout_format, error);
+	}
 	return str_c(str);
 }
 
diff --git a/src/plugins/lda-sieve/lda-sieve-log.c b/src/plugins/lda-sieve/lda-sieve-log.c
index f2b0d091163f25e174c8f30e1cb8987d7735085d..2c1ae4c4ebf4854f1aa96fff99a5041944169836 100644
--- a/src/plugins/lda-sieve/lda-sieve-log.c
+++ b/src/plugins/lda-sieve/lda-sieve-log.c
@@ -26,12 +26,16 @@ static const char *ATTR_FORMAT(2, 0) lda_sieve_log_expand_message
 	struct mail_deliver_context *mdctx = ehandler->mdctx;
 	const struct var_expand_table *table;
 	string_t *str;
+	const char *error;
 
 	table = mail_deliver_ctx_get_log_var_expand_table
 		(mdctx, t_strdup_vprintf(fmt, args));
 
 	str = t_str_new(256);
-	var_expand(str, mdctx->set->deliver_log_format, table);
+	if (var_expand(str, mdctx->set->deliver_log_format, table, &error) <= 0) {
+		i_error("Failed to expand deliver_log_format=%s: %s",
+			mdctx->set->deliver_log_format, error);
+	}
 	return str_c(str);
 }