diff --git a/Makefile.am b/Makefile.am
index c518ec9b5c3b9122758ea9e6563c2747123a43f7..7e5a978c0a3d7786a52d2e29b1b5992246029d54 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -35,6 +35,7 @@ test_cases = \
 	tests/extensions/variables/modifiers.svtest \
 	tests/extensions/variables/quoting.svtest \
 	tests/extensions/variables/string.svtest \
+	tests/extensions/variables/errors.svtest \
 	tests/extensions/include/variables.svtest \
 	tests/extensions/imapflags/basic.svtest \
 	tests/extensions/imapflags/rfc.svtest \
diff --git a/src/lib-sieve/plugins/variables/ext-variables-arguments.c b/src/lib-sieve/plugins/variables/ext-variables-arguments.c
index 146fd8f2d2fba351509ba78e4144290445846822..3f8382650c00b470b06d214e2565de992710e12e 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-arguments.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-arguments.c
@@ -31,7 +31,7 @@ const struct sieve_argument variable_argument = {
 
 static struct sieve_ast_argument *ext_variables_variable_argument_create
 (struct sieve_validator *validator, struct sieve_ast *ast, 
-	unsigned int source_line,	const char *variable)
+	unsigned int source_line, const char *variable)
 {
 	struct sieve_variable *var;
 	struct sieve_ast_argument *arg;
@@ -70,7 +70,7 @@ bool sieve_variable_argument_activate
 	
 		if ( nelements < 0 || varstr != varend ) {
 			sieve_command_validate_error(validator, cmd, 
-				"invalid variable name '%s'", varstr);
+				"invalid variable name '%s'", str_c(variable));
 		} else if ( nelements == 1 ) {
 			const struct ext_variable_name *cur_element = 
 				array_idx(&vname, 0);
@@ -84,7 +84,7 @@ bool sieve_variable_argument_activate
 				
 				result = TRUE;
 			} else {
-				if ( assignment ) {
+				if ( !assignment ) {
 					arg->argument = &match_value_argument;
 					arg->context = (void *) cur_element->num_variable;
 				
@@ -105,8 +105,8 @@ bool sieve_variable_argument_activate
 			 */
 
 			sieve_command_validate_error(validator, cmd, 
-				"cannot assign to variable in unknown namespace '%s'", 
-				str_c(cur_element->identifier));
+				"cannot %s to variable in unknown namespace '%s'", 
+				assignment ? "assign" : "refer", str_c(cur_element->identifier));
 		}
 	} T_END;
 
diff --git a/tests/extensions/variables/errors.svtest b/tests/extensions/variables/errors.svtest
new file mode 100644
index 0000000000000000000000000000000000000000..2fbdeb51633faed63b4c41353364ebf73c934d1b
--- /dev/null
+++ b/tests/extensions/variables/errors.svtest
@@ -0,0 +1,24 @@
+require "vnd.dovecot.testsuite";
+
+require "comparator-i;ascii-numeric";
+require "relational";
+
+test "Invalid namespaces (FIXME: count only)" {
+	if test_compile "errors/namespace.sieve" {
+		test_fail "compile should have failed";
+	}
+
+	if not test_error :count "eq" :comparator "i;ascii-numeric" "5" {
+		test_fail "wrong number of errors reported";
+	}
+}
+
+test "Invalid set command invocations (FIXME: count only)" {
+	if test_compile "errors/set.sieve" {
+		test_fail "compile should have failed";
+	}
+
+	if not test_error :count "eq" :comparator "i;ascii-numeric" "5" {
+		test_fail "wrong number of errors reported";
+	}
+}
diff --git a/tests/extensions/variables/errors/namespace.sieve b/tests/extensions/variables/errors/namespace.sieve
new file mode 100644
index 0000000000000000000000000000000000000000..e11ac6d78adc8b750e84aa68697297b341f0a708
--- /dev/null
+++ b/tests/extensions/variables/errors/namespace.sieve
@@ -0,0 +1,8 @@
+require "variables";
+require "fileinto";
+
+set "namespace.frop" "value";
+set "complex.struct.frop" "value";
+
+fileinto "${namespace.frop}";
+fileinto "${complex.struct.frop}";
diff --git a/tests/extensions/variables/errors/set.sieve b/tests/extensions/variables/errors/set.sieve
new file mode 100644
index 0000000000000000000000000000000000000000..f3cb5fd469187d6bb3064e36d481db24295fab46
--- /dev/null
+++ b/tests/extensions/variables/errors/set.sieve
@@ -0,0 +1,16 @@
+require "variables";
+
+# Invalid variable name
+set "${frop}" "frop";
+set "...." "frop";
+set "name." "frop";
+set ".name" "frop";
+
+# Not an error
+set "\n\a\m\e" "frop";
+
+# Trying to assign match variable;
+set "0" "frop";
+
+# Not an error
+set :UPPER "name" "frop";