Skip to content

Commit

Permalink
convert HTTPException.description to string
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed May 17, 2021
1 parent c2fd0f1 commit 67ef741
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Unreleased
characters. :issue:`2125`
- Type checking understands that calling ``headers.get`` with a string
default will always return a string. :issue:`2128`
- If ``HTTPException.description`` is not a string,
``get_description`` will convert it to a string. :issue:`2115`


Version 2.0.0
Expand Down
9 changes: 8 additions & 1 deletion src/werkzeug/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ def get_description(
scope: t.Optional[dict] = None,
) -> str:
"""Get the description."""
description = escape(self.description).replace("\n", "<br>") # type: ignore
if self.description is None:
description = ""
elif not isinstance(self.description, str):
description = str(self.description)
else:
description = self.description

description = escape(description).replace("\n", "<br>")
return f"<p>{description}</p>"

def get_body(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ class TestResponse(Response):
exc = cls(response=TestResponse())
rp = exc.get_response({})
assert isinstance(rp, TestResponse)


def test_description_none():
HTTPException().get_response()

0 comments on commit 67ef741

Please sign in to comment.