Skip to content

Commit

Permalink
Merge pull request #2133 from pallets/http-description
Browse files Browse the repository at this point in the history
convert `HTTPException.description` to string
  • Loading branch information
davidism committed May 17, 2021
2 parents c2fd0f1 + 67ef741 commit e6305ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
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
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
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 e6305ec

Please sign in to comment.