Skip to content

Commit

Permalink
Fix werkzeug 2.1.0 import & dev tools error html rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Mar 29, 2022
1 parent e87339f commit 2e5b21c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function UnconnectedErrorContent({error, base}) {
)}
{/* Backend Error */}
{typeof error.html !== 'string' ? null : error.html.indexOf(
'<!DOCTYPE HTML'
'<!DOCTYPE'
) === 0 ? (
<div className='dash-be-error__st'>
<div className='dash-backend-error'>
Expand Down
41 changes: 32 additions & 9 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import flask
from flask_compress import Compress
from werkzeug.debug.tbtools import get_current_traceback

from werkzeug.debug import tbtools
from werkzeug.security import gen_salt

from pkg_resources import get_distribution, parse_version
from dash import dcc
from dash import html
Expand Down Expand Up @@ -102,6 +105,29 @@
_re_renderer_scripts_id = 'id="_dash-renderer', "new DashRenderer"


def _get_traceback(secret, error):
def _get_skip(text):
skip = 0
for i, line in enumerate(text.splitlines()):
if "%% callback invoked %%" in line:
skip = int((i + 1) / 2)
break
return skip

# werkzeug<2.1.0
if hasattr(tbtools, "get_current_traceback"):
tb = tbtools.get_current_traceback()
skip = _get_skip(tb.plaintext)
return tbtools.get_current_traceback(skip=skip).render_full()

tb = tbtools.DebugTraceback(error)
skip = _get_skip(tb.render_traceback_text())

return tbtools.DebugTraceback(error, skip=skip).render_debugger_html(
True, secret, True
)


class _NoUpdate:
# pylint: disable=too-few-public-methods
pass
Expand Down Expand Up @@ -1756,19 +1782,16 @@ def enable_dev_tools(

if debug and dev_tools.prune_errors:

secret = gen_salt(20)

@self.server.errorhandler(Exception)
def _wrap_errors(_):
def _wrap_errors(error):
# find the callback invocation, if the error is from a callback
# and skip the traceback up to that point
# if the error didn't come from inside a callback, we won't
# skip anything.
tb = get_current_traceback()
skip = 0
for i, line in enumerate(tb.plaintext.splitlines()):
if "%% callback invoked %%" in line:
skip = int((i + 1) / 2)
break
return get_current_traceback(skip=skip).render_full(), 500
tb = _get_traceback(secret, error)
return tb, 500

if debug and dev_tools.ui:

Expand Down

0 comments on commit 2e5b21c

Please sign in to comment.