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

Get websocket data in exception handler #100

Closed
Romainpaulus opened this issue Mar 11, 2015 · 4 comments
Closed

Get websocket data in exception handler #100

Romainpaulus opened this issue Mar 11, 2015 · 4 comments

Comments

@Romainpaulus
Copy link
Contributor

I want to access the websocket data for exception logging, similarly to this example:

socketio = SocketIO()

@socketio.on_error_default
def error_handler(e):
    # Log the exception with event data
    pass

@socketio.on('my event')
def message_hander(data):
    raise RuntimeError()

From what I have seen so far, there is no simple way to get the data that was passed to the message_handler function inside the error_handler, unless overriding the socketio.on decorator. I want to submit a pull request that will achieve this, but I want to know first if there is a way to do this without changing the socketio.on method. Is there, for instance, a request attribute that contains the current websocket message and data?

@miguelgrinberg
Copy link
Owner

This is a good idea, I like it.

You need to keep in mind that there are a handful of events, and they all have different arguments. You have message (payload is a string), json (payload is a JSON blob), user-defined events (JSON payload), connect and disconnect.

The easiest way to achieve what you want is to add all the data to request. You can see here how I added the namespace. Maybe call it request.event and have it be a dict, something like this:

{
    'message': <message name>,
    'args': <arg list>
}

The message name can be 'message', 'json', 'connect', 'disconnect' or a custom event name. The args can be a list of args if available, or None if the event does not take arguments.

Would something like this work for you?

@Romainpaulus
Copy link
Contributor Author

Sounds good, I'm going to work on this.

@miguelgrinberg
Copy link
Owner

The upcoming version 1.0 will support this. You can use request.event to get a dictionary that contains two keys, message and args.

@miguelgrinberg
Copy link
Owner

Closing, since release 1.0 has been released.

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

No branches or pull requests

2 participants