Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncWebsocketConsumer: KeyError "code". #1926

Open
VladosikPaltosik opened this issue Oct 6, 2022 · 2 comments
Open

AsyncWebsocketConsumer: KeyError "code". #1926

VladosikPaltosik opened this issue Oct 6, 2022 · 2 comments

Comments

@VladosikPaltosik
Copy link

channels/generic/websocket.py in websocket_disconnect at line 238

                await self.channel_layer.group_discard(group, self.channel_name)
        except AttributeError:
            raise InvalidChannelLayerError(
                "BACKEND is unconfigured or doesn't support groups"
            )
        await self.disconnect(message["code"])
        raise StopConsumer()

Got KeyError "code". How to solve a problem???

@carltongibson
Copy link
Member

This would be addressed by #1905, which is lacking a regression test currently.

@VladosikPaltosik
Copy link
Author

VladosikPaltosik commented Oct 7, 2022

This would be addressed by #1905, which is lacking a regression test currently.

Perhaps I can handle this error? Because my Sentry are full of such errors.

class ConnectionConsumer(AsyncWebsocketConsumer):

    async def connect(self):
        """ Called on connection """
        try:
            await self.accept()
        except (ConnectionClosedError, ConnectionClosedOK, KeyError):
            await self.close(code=1005)

    async def receive(self, text_data=None, bytes_data=None):
        """ Receive a message and send it back """
        text_data_json = json.loads(text_data)
        message = text_data_json.get('message', None)
        try:
            await self.send(text_data=json.dumps({'message': message}))
        except (ConnectionClosedError, ConnectionClosedOK, KeyError):
            await self.close(code=1005)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants