strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.01k stars 531 forks source link

`MaskErrors` does not mask errors for subscriptions #3680

Open axiomofjoy opened 2 weeks ago

axiomofjoy commented 2 weeks ago

The MaskErrors schema extension does not seem to mask errors for subscriptions. I am running on the latest version of strawberry. Example included below:

from typing import AsyncIterator
import strawberry
from strawberry.extensions import MaskErrors

@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        raise Exception("boom")
        return "world"

@strawberry.type
class Subscription:
    @strawberry.subscription
    async def stream(self) -> AsyncIterator[str]:
        yield "hello"
        yield "world"
        raise Exception("boom")

schema = strawberry.Schema(
    query=Query, subscription=Subscription, extensions=[MaskErrors()]
)

The errors are masked for queries:

Image

But not for subscriptions:

Image

Describe the Bug

I expect the MaskErrors extension to mask my errors for subscriptions as well as queries and mutations, but it does not appear to do so.

System Information

Upvote & Fund

Fund with Polar

Speedy1991 commented 2 weeks ago

Can confirm this - the problem is, a subscriptions never sets an execution_context.result (it is always None) and therefore the error check failes.

I think the problem is send_next because it just formats the error and in line 381 it send the message without going through the gql middlewares

Same problem in the graphql-ws implementation here