Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
457 stars 493 forks source link

DeviceNotFoundException results in InvalidErrorCode (404103) #3442

Open pregress opened 4 months ago

pregress commented 4 months ago

Context

Description of the issue

https://github.com/Azure/azure-iot-sdk-csharp/blob/main/iothub/service/src/Common/Exceptions/ErrorCode.cs should contain a value for 404103 DeviceOffline

When invoking a direct method on an offline device. A device not found is thrown. OK The exception.ErrorCode is InvalidErrorCode.

Whilst the json contains: errorCode: 404103

{"Message":"{\"errorCode\":404103,\"trackingId\":\"aa761f96e9174d0e849c96a8b2e10a8c-TimeStamp:03/14/2024 09:54:49\",\"message\":\"The operation failed because the requested device isn't online or hasn't registered the direct method callback. To learn more, see https://aka.ms/iothub404103\",\"info\":{\"timeout\":\"00:00:00\"},\"timestampUtc\":\"2024-03-14T09:54:49.6536567Z\"}","ExceptionMessage":""}

Code sample exhibiting the issue

try
{
   var credential = new AzureCliCredential();
   var serviceClient = ServiceClient.Create($"redacted-hostname.azure-devices.net", credential);
   await serviceClient.InvokeDeviceMethodAsync("YourDevice",  new CloudToDeviceMethod("YourDirectMethod", TimeSpan.FromSeconds(10)));
}
catch (DeviceNotFoundException ex)
{   
    Console.WriteLine("Offline? " +  ex.Code);
}

Console log of the issue

Offline? InvalidErrorCode

pregress commented 3 months ago

Merged in: https://github.com/Azure/azure-iot-sdk-csharp/pull/3450

Alexolo commented 1 month ago

I'm also interested in this, an correct me if im wrong, but merge #3450, only solves it into v2, not v1 nugets.