Catch mako time errors with Bottle

I am looking for a way to catch mako runtime errors with Bottle.

Runtime errors in python are fixed with the following code:

# main.py from lib import errors import bottle app = bottle.app() app.error_handler = errors.handler ... # lib/errors.py from bottle import mako_template as template def custom500(error): return template('error/500') handler = { 500: custom500 } 

This works flawlessly as exceptions turn into a 500 Internal Server Error.

I would like similar errors to display mako time errors, does anyone know how to do this?

+4
source share
1 answer

You want to catch mako.exceptions.SyntaxException .

This code works for me:

 @bottle.route('/hello') def hello(): try: return bottle.mako_template('hello') except mako.exceptions.SyntaxException as exx: return 'mako exception: {}\n'.format(exx) 

EDIT: for your comment, here are a few pointers on how to set this globally. Install a plugin that wraps your functions in mako.exceptions.SyntaxException try block.

Something like that:

 @bottle.route('/hello') def hello(): return bottle.mako_template('hello') def catch_mako_errors(callback): def wrapper(*args, **kwargs): try: return callback(*args, **kwargs) except mako.exceptions.SyntaxException as exx: return 'mako exception: {}\n'.format(exx) return wrapper bottle.install(catch_mako_errors) 
+3
source

Source: https://habr.com/ru/post/1499550/


All Articles