benselme / flask-mako

Provides support for Mako Templates in Flask.
40 stars 30 forks source link

improve error handling #2

Open kofrasa opened 12 years ago

kofrasa commented 12 years ago

Problem

Currently, all mako exceptions are intercepted and wrapped by the TemplateError. This is not too useful as the mako error messages are suppressed and the stack trace is not provided in an easily readable presentation.

Template errors will typically be dumped onto the browser to allow easy navigation of the call stack

Suggestion

Expose the underlying Mako errors as a callableTemplateError returning the stack trace as HTML. Changes are shown below, with the extra imports

from flask.helpers import make_response
from mako.exceptions import html_error_template

class TemplateError(RuntimeError):
    """ A template has thrown an error during rendering. """
    def __init__(self, template):
        self.tb = RichTraceback()
        self.text = text_error_template().render()
        self.html = html_error_template().render()
        msg = "Error occurred while rendering template '{0}'"
        msg = msg.format(template.uri)
        super(TemplateError, self).__init__(msg)

    def __call__(self, environ, start_response):
        """Call the exception as WSGI application.

        :param environ: the WSGI environment.
        :param start_response: the response callable provided by the WSGI
                               server.
        """
        response = make_response(self.html)
        return response(environ, start_response)
AnIrishDuck commented 12 years ago

This might be a useful half measure. However, the full solution (translate werkzeug traceback so that the interactive debugger shows proper source, variables, etc) has been committed already (see 0b537235fb125f427de7ab4acbbe5f6f6f2a258d). This requires a minor change to werkzeug (see mitsuhiko/werkzeug#206) that hasn't been committed yet.

jab commented 9 years ago

Sorry to see this issue stalled for so long, this would be such a huge improvement!! Even if just the "half-measure" above were added.

@AnIrishDuck, looks like your PR was just a testcase and changelog entry away from getting merged, but it never happened (see https://github.com/mitsuhiko/werkzeug/pull/206#issuecomment-53995120). Are you still interested in this? And are you, @benselme, still maintaining this project?

I'd be happy to help with the remaining work, but first just want to ask how realistic it is that this will get any attention here and make it into a new release soon.

Thanks!

jab commented 9 years ago

Just applied the changes from https://github.com/mitsuhiko/werkzeug/pull/206/files to Werkzeug manually and it looks like flask-mako needs no further modification for this to work, amazing!

In that case, if my comment https://github.com/mitsuhiko/werkzeug/pull/206#issuecomment-150024212 gets a response from the Werkzeug maintainers soon, I can volunteer to do the last bit of work to get that merged into Werkzeug, if you don't beat me to it.

benselme commented 9 years ago

I'm not really maintaining anything here but I'll be happy to make a release if it helps !

jab commented 9 years ago

Thanks @benselme! Fortunately it looks like all it will take before this issue can be closed is mitsuhiko/werkzeug#206 to be merged; flask-mako already has what it needs thanks to @AnIrishDuck's https://github.com/benselme/flask-mako/commit/0b537235fb125f427de7ab4acbbe5f6f6f2a258d. But great you're still able to make new releases! Looks like it's been 2 years since the last release, and since then #8 and #9 were fixed/added, so might be worth making a new release now?