diff --git a/CHANGES.rst b/CHANGES.rst index 8a8af6e9b..7444a2014 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/werkzeug/exceptions.py b/src/werkzeug/exceptions.py index 7c43d85ef..a3e3b0796 100644 --- a/src/werkzeug/exceptions.py +++ b/src/werkzeug/exceptions.py @@ -156,7 +156,14 @@ def get_description( scope: t.Optional[dict] = None, ) -> str: """Get the description.""" - description = escape(self.description).replace("\n", "
") # 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", "
") return f"

{description}

" def get_body( diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 12ea8b920..2a6839057 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -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()