Skip to content

Commit

Permalink
Merge pull request #31 from alex-oleshkevich/safe-str
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oleshkevich committed Jun 21, 2023
2 parents 327ef75 + 088b51c commit b68b6ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions examples/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ class WithHintError(Exception):


def index_view(request: Request) -> typing.NoReturn:
class StrError:
"""Value that raises exception during conversion to string."""

def __str__(self) -> str:
raise ValueError('hahaha crash <b>me</b>')

token = 'mytoken' # noqa
request.state.token = 'mytoken'

request.app.state.app_token = 'app mytoken'
request.state.error = StrError()
request.app.state.error = StrError()
raise ValueError('This is the first cause')


Expand Down
8 changes: 8 additions & 0 deletions starception/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,18 @@ def highlight(value: str, filename: str) -> str:
return markupsafe.escape(value)


def save_str(value: typing.Any) -> str:
try:
return str(value)
except Exception as ex:
return Markup(f'<span class="text-error">ERROR</span>: {html.escape(str(ex))}')


jinja = jinja2.Environment(loader=jinja2.PackageLoader(__name__))
jinja.filters.update(
{
'symbol': get_symbol,
'save_str': save_str,
'frame_id': frame_id,
'is_vendor': is_vendor,
'highlight': highlight,
Expand Down
2 changes: 1 addition & 1 deletion starception/templates/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% for label, value in items.items() %}
<dl>
<dt>{{ label }}</dt>
<dd>{{ value|mask_secrets(label)|escape }}</dd>
<dd>{{ value|save_str|mask_secrets(label)|escape }}</dd>
</dl>
{% else %}
<dl>
Expand Down
4 changes: 4 additions & 0 deletions starception/templates/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,7 @@ dd {
.search-links a:hover {
text-decoration: underline;
}

.text-error {
color: var(--red);
}

0 comments on commit b68b6ea

Please sign in to comment.