diff --git a/product/ERP5/Tool/TemplateTool.py b/product/ERP5/Tool/TemplateTool.py
index 7c799d7e8a6b9bc9149e5702ad291b6d2c465cef..20a5c06b22a2f5dd5f8686a98e2f4ccb23feae4f 100644
--- a/product/ERP5/Tool/TemplateTool.py
+++ b/product/ERP5/Tool/TemplateTool.py
@@ -45,6 +45,7 @@ from cStringIO import StringIO
 from urllib import pathname2url, urlopen, splittype, urlretrieve
 import re
 from xml.dom.minidom import parse
+from xml.parsers.expat import ExpatError
 import struct
 import cPickle
 import posixpath
@@ -526,7 +527,17 @@ class TemplateTool (BaseTool):
         f = urlopen(url)
         property_dict_list = []
         try:
-          doc = parse(f)
+          try:
+            doc = parse(f)
+          except ExpatError:
+            if REQUEST is not None:
+              psm = translateString('Invalid repository: ${repo}',
+                                    mapping={'repo':repository})
+              REQUEST.RESPONSE.redirect("%s?portal_status_message=%s"
+                                       % (self.absolute_url(), psm))
+              return
+            else:
+              raise RuntimeError, 'Invalid repository: %s' % repository
           try:
             root = doc.documentElement
             for template in root.getElementsByTagName("template"):