Skip to content

Commit 2437857

Browse files
mgorvenbellini666
andauthored
Fix Content-Type for HTTP exceptions (#4037)
Co-authored-by: Thiago Bellini Ribeiro <[email protected]>
1 parent 2865fea commit 2437857

File tree

8 files changed

+29
-3
lines changed

8 files changed

+29
-3
lines changed

RELEASE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Release type: patch
2+
3+
Set Content-Type to `text/plain` for exceptions so that these are displayed
4+
correctly.

strawberry/chalice/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def execute_request(self, request: Request) -> Response:
8282
return Response(
8383
body=e.reason,
8484
status_code=e.status_code,
85+
headers={"Content-Type": "text/plain"},
8586
)
8687

8788

strawberry/channels/handlers/http_handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ async def handle(self, body: bytes) -> None:
240240
else:
241241
assert_never(response)
242242
except HTTPException as e:
243-
await self.send_response(e.status_code, e.reason.encode())
243+
await self.send_response(
244+
e.status_code,
245+
e.reason.encode(),
246+
headers=[(b"Content-Type", b"text/plain")],
247+
)
244248

245249

246250
class GraphQLHTTPConsumer(

strawberry/django/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def dispatch(
164164
return HttpResponse(
165165
content=e.reason,
166166
status=e.status_code,
167+
content_type="text/plain",
167168
)
168169

169170
def render_graphql_ide(self, request: HttpRequest) -> HttpResponse:
@@ -224,6 +225,7 @@ async def dispatch( # pyright: ignore
224225
return HttpResponse(
225226
content=e.reason,
226227
status=e.status_code,
228+
content_type="text/plain",
227229
)
228230

229231
async def render_graphql_ide(self, request: HttpRequest) -> HttpResponse:

strawberry/flask/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def dispatch_request(self) -> ResponseReturnValue:
8484
return Response(
8585
response=e.reason,
8686
status=e.status_code,
87+
content_type="text/plain",
8788
)
8889

8990
def render_graphql_ide(self, request: Request) -> Response:
@@ -117,6 +118,7 @@ async def dispatch_request(self) -> ResponseReturnValue: # type: ignore
117118
return Response(
118119
response=e.reason,
119120
status=e.status_code,
121+
content_type="text/plain",
120122
)
121123

122124
async def render_graphql_ide(self, request: Request) -> Response:

strawberry/quart/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ async def dispatch_request(self, **kwargs: object) -> "ResponseReturnValue":
145145
return Response(
146146
response=e.reason,
147147
status=e.status_code,
148+
content_type="text/plain",
148149
)
149150

150151
async def create_streaming_response(

strawberry/sanic/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,19 @@ async def post(self, request: Request) -> HTTPResponse:
138138
try:
139139
return await self.run(request)
140140
except HTTPException as e:
141-
return HTTPResponse(e.reason, status=e.status_code)
141+
return HTTPResponse(
142+
e.reason, status=e.status_code, content_type="text/plain"
143+
)
142144

143145
async def get(self, request: Request) -> HTTPResponse:
144146
self.request = request
145147

146148
try:
147149
return await self.run(request)
148150
except HTTPException as e:
149-
return HTTPResponse(e.reason, status=e.status_code)
151+
return HTTPResponse(
152+
e.reason, status=e.status_code, content_type="text/plain"
153+
)
150154

151155
async def create_streaming_response(
152156
self,

tests/http/test_http.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ async def test_the_http_handler_uses_the_views_decode_json_method(
2424

2525
response = await http_client.query(query="{ hello }")
2626
assert response.status_code == 200
27+
assert response.headers["content-type"].split(";")[0] == "application/json"
2728

2829
data = response.json["data"]
2930
assert isinstance(data, dict)
3031
assert data["hello"] == "Hello world"
3132

3233
assert spy.call_count == 1
34+
35+
36+
async def test_exception(http_client: HttpClient, mocker):
37+
response = await http_client.query(query="{ hello }", operation_name="wrong")
38+
assert response.status_code == 400
39+
assert response.headers["content-type"].split(";")[0] == "text/plain"
40+
assert response.data == b'Unknown operation named "wrong".'

0 commit comments

Comments
 (0)