From 67ef7411a0e7add1192c23aa6fd63eb299e8217e Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 17 May 2021 15:52:51 -0700 Subject: [PATCH] convert `HTTPException.description` to string --- CHANGES.rst | 2 ++ src/werkzeug/exceptions.py | 9 ++++++++- tests/test_exceptions.py | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) 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()