diff --git a/girepository/girparser.c b/girepository/girparser.c
index 041c65d..949289c 100644
--- a/girepository/girparser.c
+++ b/girepository/girparser.c
@@ -3511,10 +3512,11 @@ _g_ir_parser_parse_string (GIrParser           *parser,
   if (ctx.modules)
     return ctx.modules->data;
 
-  g_set_error (error,
-               G_MARKUP_ERROR,
-               G_MARKUP_ERROR_INVALID_CONTENT,
-               "Expected namespace element in the gir file");
+  if (*error == NULL)
+    g_set_error (error,
+                 G_MARKUP_ERROR,
+                 G_MARKUP_ERROR_INVALID_CONTENT,
+                 "Expected namespace element in the gir file");
   return NULL;
 }
 
