Changeset 8

Show
Ignore:
Timestamp:
Wed Jun 22 03:46:29 2005
Author:
rdelon
Message:

Added better error reporting when rendering fails, to make it easier to debug the problem

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/cherrytemplate/cherrytemplate.py

    r7 r8  
    23 23 _quote3 = '"""'  
    24 24  
      25 class RenderError(Exception): pass  
    25 26 class ParseError(Exception): pass  
    26 27 class InternalError(Exception): pass  
     
    444 445 def renderTemplate(template = '', file = None, inputEncoding = None, outputEncoding = None, outputEncodingErrors = None, returnGenerator = None, glob = None, loc = None):  
    445 446     # print "* Rendering:", file  
      447     originalTemplate = template  
    446 448     if loc is None:  
    447 449         loc = inspect.currentframe(1).f_locals  
     
    457 459     _writeTemplate(f, template, '    ')  
    458 460     template = f.getvalue()  
    459       # print "*** template:", template  
    460       # print "*** END TEMPLATE"  
    461 461      
    462 462     g = glob.copy() # make a copy because we don't want to avoid changing original global scope.  
     
    472 472         returnGenerator = defaultReturnGenerator  
    473 473  
    474       result = eval('_renderTemplate()',g)  
      474     try:  
      475         result = eval('_renderTemplate()',g)  
    475 476  
    476       if returnGenerator:  
    477           return _resultAsGenerator(result, inputEncoding, outputEncoding, outputEncodingErrors)  
    478       else:  
    479           result = ''.join(list(result))  
    480           if not isinstance(result, unicode):  
    481               if inputEncoding:  
    482                   result = unicode(result, inputEncoding)  
    483           if outputEncoding:  
    484               return result.encode(outputEncoding, outputEncodingErrors)  
    485           return result  
      477         if returnGenerator:  
      478             return _resultAsGenerator(result, inputEncoding, outputEncoding, outputEncodingErrors)  
      479         else:  
      480             result = ''.join(list(result))  
      481             if not isinstance(result, unicode):  
      482                 if inputEncoding:  
      483                     result = unicode(result, inputEncoding)  
      484             if outputEncoding:  
      485                 return result.encode(outputEncoding, outputEncodingErrors)  
      486             return result  
      487     except:  
      488         # In case of an exception, we include the body of the template in  
      489         #   the traceback  
      490         import sys, traceback  
      491         tb = "".join(traceback.format_exception(*sys.exc_info()))  
      492         errorList = ["An error occured while trying to render a template."]  
      493         if file is not None:  
      494             errorList.append("The template file was %s" % repr(file))  
      495         errorList.append("The traceback was:")  
      496         errorList.append(_indentAndNumberCode(tb, number = False))  
      497         errorList.append("The template code was:")  
      498         errorList.append(_indentAndNumberCode(template))  
      499         if file is None:  
      500             errorList.append("The original template was:")  
      501             errorList.append(_indentAndNumberCode(originalTemplate, number = False))  
      502         raise RenderError, '\n'.join(errorList)  
    486 503  
    487 504 def _resultAsGenerator(result, inputEncoding, outputEncoding, outputEncodingErrors):  
     
    494 511             yield line  
    495 512  
      513 def _indentAndNumberCode(code, number = True, tab = '    '):  
      514     resList = []  
      515     for i, line in enumerate(code.splitlines()):  
      516         if number:  
      517             line = '%05d' % (i+1) + ' ' + line  
      518         resList.append(tab +  line)  
      519     return '\n'.join(resList)