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

hcshmk/invocation_id - Read invocation_id from method parameters, letting user override it if desired. #65

Merged
merged 2 commits into from Feb 27, 2022

Conversation

ghost
Copy link

@ghost ghost commented Jun 23, 2021

This merge request lets the user supply their own invocation_id to the BaseHubConnection.send method. The purpose is to allow the user to easily perform error handling on individual requests/calls/invocations by registering an on_error event handler that can take advantage of this, as demonstrated below:

# Dict pairing uuid with a function
error_handlers: Dict[UUID, Callable[[str], None]] = dict()

# Handle server-sent exceptions by looking up uuid for that particular call/invocation
def on_error(self, completion_message: CompletionMessage):
    uuid = UUID(completion_message.invocation_id)
    error = completion_message.error
    handler = error_handlers[uuid]
    del error_handlers[uuid]
    handler(error)
connection.on_error(on_error)

# Call hub function with simple error handling
single_use_uuid = uuid.uuid4()
error_handlers[uuid] = lambda error_msg: print(f'This specific call to DoStuff failed with message: {error_msg}')
connection.send(method = 'DoStuff',
                arguments = [],
                on_invocation = lambda response: on_success(response.result),
                invocation_id=str(single_use_uuid))

Demo code is kept brief and simple, just for proof of concept. With a few extra lines and a wrapper function, you can get the send function to raise an exception in python whenever invoke fails serverside.

@ghost
Copy link
Author

ghost commented Jul 12, 2021

Mentioning @sillydan1

@mandrewcito mandrewcito merged commit 02b47e6 into mandrewcito:main Feb 27, 2022
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

Successfully merging this pull request may close these issues.

1 participant