Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Release type: patch

Set Content-Type to `text/plain` for exceptions so that these are displayed
correctly.
1 change: 1 addition & 0 deletions strawberry/chalice/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def execute_request(self, request: Request) -> Response:
return Response(
body=e.reason,
status_code=e.status_code,
headers={"Content-Type": "text/plain"},
)


Expand Down
6 changes: 5 additions & 1 deletion strawberry/channels/handlers/http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ async def handle(self, body: bytes) -> None:
else:
assert_never(response)
except HTTPException as e:
await self.send_response(e.status_code, e.reason.encode())
await self.send_response(
e.status_code,
e.reason.encode(),
headers=[(b"Content-Type", b"text/plain")],
)


class GraphQLHTTPConsumer(
Expand Down
2 changes: 2 additions & 0 deletions strawberry/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def dispatch(
return HttpResponse(
content=e.reason,
status=e.status_code,
content_type="text/plain",
)

def render_graphql_ide(self, request: HttpRequest) -> HttpResponse:
Expand Down Expand Up @@ -224,6 +225,7 @@ async def dispatch( # pyright: ignore
return HttpResponse(
content=e.reason,
status=e.status_code,
content_type="text/plain",
)

async def render_graphql_ide(self, request: HttpRequest) -> HttpResponse:
Expand Down
2 changes: 2 additions & 0 deletions strawberry/flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def dispatch_request(self) -> ResponseReturnValue:
return Response(
response=e.reason,
status=e.status_code,
content_type="text/plain",
)

def render_graphql_ide(self, request: Request) -> Response:
Expand Down Expand Up @@ -117,6 +118,7 @@ async def dispatch_request(self) -> ResponseReturnValue: # type: ignore
return Response(
response=e.reason,
status=e.status_code,
content_type="text/plain",
)

async def render_graphql_ide(self, request: Request) -> Response:
Expand Down
1 change: 1 addition & 0 deletions strawberry/quart/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ async def dispatch_request(self, **kwargs: object) -> "ResponseReturnValue":
return Response(
response=e.reason,
status=e.status_code,
content_type="text/plain",
)

async def create_streaming_response(
Expand Down
8 changes: 6 additions & 2 deletions strawberry/sanic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ async def post(self, request: Request) -> HTTPResponse:
try:
return await self.run(request)
except HTTPException as e:
return HTTPResponse(e.reason, status=e.status_code)
return HTTPResponse(
e.reason, status=e.status_code, content_type="text/plain"
)

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

try:
return await self.run(request)
except HTTPException as e:
return HTTPResponse(e.reason, status=e.status_code)
return HTTPResponse(
e.reason, status=e.status_code, content_type="text/plain"
)

async def create_streaming_response(
self,
Expand Down
8 changes: 8 additions & 0 deletions tests/http/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ async def test_the_http_handler_uses_the_views_decode_json_method(

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

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

assert spy.call_count == 1


async def test_exception(http_client: HttpClient, mocker):
response = await http_client.query(query="{ hello }", operation_name="wrong")
assert response.status_code == 400
assert response.headers["content-type"].split(";")[0] == "text/plain"
assert response.data == b'Unknown operation named "wrong".'
Loading