more robust exception handling in on_message
better function signature