diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py
index 267991e..4e99c1d 100644
--- a/scripts/qapi-commands.py
+++ b/scripts/qapi-commands.py
@@ -25,17 +25,6 @@ def gen_command_decl(name, arg_type, ret_type):
                  params=gen_params(arg_type, 'Error **errp'))
 
 
-def gen_err_check(err):
-    if not err:
-        return ''
-    return mcgen('''
-    if (%(err)s) {
-        goto out;
-    }
-''',
-                 err=err)
-
-
 def gen_call(name, arg_type, ret_type):
     ret = ''
 
@@ -56,7 +45,7 @@ def gen_call(name, arg_type, ret_type):
 ''',
                 c_name=c_name(name), args=argstr, lhs=lhs)
     if ret_type:
-        ret += gen_err_check('err')
+        ret += gen_err_check()
         ret += mcgen('''
 
     qmp_marshal_output_%(c_name)s(retval, ret, &err);
@@ -133,7 +122,7 @@ def gen_marshal_input_visit(arg_type, dealloc=False):
 ''',
                          c_name=c_name(memb.name), name=memb.name,
                          errp=errparg)
-            ret += gen_err_check(errarg)
+            ret += gen_err_check(err=errarg)
             ret += mcgen('''
     if (has_%(c_name)s) {
 ''',
@@ -144,7 +133,7 @@ def gen_marshal_input_visit(arg_type, dealloc=False):
 ''',
                      c_name=c_name(memb.name), name=memb.name,
                      c_type=memb.type.c_name(), errp=errparg)
-        ret += gen_err_check(errarg)
+        ret += gen_err_check(err=errarg)
         if memb.optional:
             pop_indent()
             ret += mcgen('''
diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py
index b5a9d4f..eaaac05 100644
--- a/scripts/qapi-event.py
+++ b/scripts/qapi-event.py
@@ -68,12 +68,9 @@ def gen_event_send(name, arg_type):
 
     /* Fake visit, as if all members are under a structure */
     visit_start_struct(v, NULL, "", "%(name)s", 0, &err);
-    if (err) {
-        goto out;
-    }
-
 ''',
                      name=name)
+        ret += gen_err_check()
 
         for memb in arg_type.members:
             if memb.optional:
@@ -91,14 +88,12 @@ def gen_event_send(name, arg_type):
 
             ret += mcgen('''
     visit_type_%(c_type)s(v, %(cast)s&%(c_name)s, "%(name)s", &err);
-    if (err) {
-        goto out;
-    }
 ''',
                          cast=cast,
                          c_name=c_name(memb.name),
                          c_type=memb.type.c_name(),
                          name=memb.name)
+            ret += gen_err_check()
 
             if memb.optional:
                 pop_indent()
@@ -107,7 +102,6 @@ def gen_event_send(name, arg_type):
 ''')
 
         ret += mcgen('''
-
     visit_end_struct(v, &err);
     if (err) {
         goto out;
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 78ad556..bc6911f 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -81,11 +81,9 @@ static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **e
     if base:
         ret += mcgen('''
     visit_type_implicit_%(c_type)s(v, &(*obj)->%(c_name)s, &err);
-    if (err) {
-        goto out;
-    }
 ''',
                      c_type=base.c_name(), c_name=c_name('base'))
+        ret += gen_err_check()
 
     for memb in members:
         if memb.optional:
@@ -107,11 +105,7 @@ static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **e
             ret += mcgen('''
     }
 ''')
-        ret += mcgen('''
-    if (err) {
-        goto out;
-    }
-''')
+        ret += gen_err_check()
 
     if re.search('^ *goto out;', ret, re.MULTILINE):
         ret += mcgen('''
@@ -271,11 +265,9 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
     if base:
         ret += mcgen('''
     visit_type_%(c_name)s_fields(v, obj, &err);
-    if (err) {
-        goto out_obj;
-    }
 ''',
                      c_name=c_name(name))
+        ret += gen_err_check(label='out_obj')
 
     tag_key = variants.tag_member.name
     if not variants.tag_name:
diff --git a/scripts/qapi.py b/scripts/qapi.py
index c0728d7..62a415c 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1536,6 +1536,18 @@ def gen_params(arg_type, extra):
         ret += sep + extra
     return ret
 
+
+def gen_err_check(err='err', label='out'):
+    if not err:
+        return ''
+    return mcgen('''
+    if (%(err)s) {
+        goto %(label)s;
+    }
+''',
+                 err=err, label=label)
+
+
 #
 # Common command line parsing
 #
