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

break(iot-dev): Rework thrown exceptions, add new device reconnection sample #1502

Merged
merged 26 commits into from
Mar 30, 2022

Conversation

timtay-microsoft
Copy link
Member

@timtay-microsoft timtay-microsoft commented Mar 24, 2022

All of DeviceClient, ModuleClient and MultiplexingClient will now share an exception class

public class IotHubClientException extends Exception
{
    @Getter
    boolean isRetryable;

    @Getter
    IotHubStatusCode iotHubStatusCode;
}

We'll still expose the IotHubStatusCode so users can catch for each specific error scenario that they are worried about and have a more general catch for scenarios they are less worried about. For instance:

try
{
    deviceClient.sendEvent(new Message("some message"));
}
catch (IotHubClientException e)
{
    if (e.isRetryable())
    {
        // retry logic
    }
    else
    {
        if (e.getStatusCode() == MESSAGE_TOO_LARGE)
        {
            // break message into smaller chunks
        }
    }
}

In general, here is the before and after for non-open/close operations:

// current sync pattern
public IotHubStatusCode sendEvent(Message message) throws InterruptedException, TimeoutException, IllegalStateException

// current async pattern
public void sendEventAsync(Message message, IotHubEventCallback callback, Object context) throws IllegalStateException 

interface IotHubEventCallback 
{
    void execute(IotHubStatusCode responseStatus, Object context);
}
// proposed sync pattern
public void sendEvent(Message message) throws InterruptedException, IllegalStateException, IotHubClientException

// proposed async pattern
public void sendEventAsync(Message message, IotHubEventCallback callback, Object context) throws IllegalStateException

interface IotHubEventCallback 
{
    void execute(IotHubStatusCode responseStatus, IotHubClientException e, Object context);
}

I want to stop returning IotHubStatusCodes to the user and expecting them to write logic to interpret them, so instead of returning the status code in the sync methods, we'll throw the IotHubClientException derived from the status code instead. Similarly, we'll include that exception in the async callback alongside the status code so that users can ignore the status code and just check the exception.

As a part of this PR, I've included my take on the new "device reconnection sample" that demonstrates best practices for keeping the connection alive and how to retry on each particular operation. It leans heavily on the proposed exception structure. It may be easiest to review the exception structure changes by reviewing this sample

I also renamed a few callback interfaces to keep the subscribeToXYZ() methods consistent with each other

I also added the sent message as a parameter in the callback for when a message was acknowledged by the service

Once this code is signed off, I plan on deleting the current device reconnection sample and replacing it with the new sample I wrote. That way, any user that already had a link to our device reconnection sample will still have a link to our recommended patterns.

@@ -13,7 +14,7 @@
OK,
BAD_FORMAT,
UNAUTHORIZED,
TOO_MANY_DEVICES,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrectly labeled when this enum was first created to represent 403 errors

@Azure Azure deleted a comment from azure-pipelines bot Mar 29, 2022
@Azure Azure deleted a comment from azure-pipelines bot Mar 29, 2022
@Azure Azure deleted a comment from azure-pipelines bot Mar 29, 2022
@Azure Azure deleted a comment from azure-pipelines bot Mar 29, 2022
@Azure Azure deleted a comment from azure-pipelines bot Mar 29, 2022
@timtay-microsoft
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@timtay-microsoft
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@timtay-microsoft
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@timtay-microsoft
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@timtay-microsoft
Copy link
Member Author

Using admin privileges to squash and merge this without horton passing since I need to make changes to horton like we've done a few times now.

@timtay-microsoft timtay-microsoft merged commit 04b0a74 into main Mar 30, 2022
@timtay-microsoft timtay-microsoft deleted the timtay/exceptions2 branch March 30, 2022 02:53
Copy link
Contributor

@azabbasi azabbasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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.

3 participants