Azure / Azure/azure-iot-sdk-csharp

DeviceNotFoundException results in InvalidErrorCode (404103)

Open
#3,442 2 comments 1 reaction 0 assignees View on GitHub
bug fix-checked-in
Dominant language
C#
Stars
477
Forks
492
Avg merge
9h 55m
Merged PRs (30d)
2

Description

# Context

- **OS, version, SKU and CPU architecture used:** Windows 11 Desktop x64 OS build: 22621.3155
- **Application's .NET Target Framework :** .NET Sdk: 8.0.101
- **Device:** Laptop
- **SDK version used:** Azure.Identity 1.10.4, Microsoft.Azure.Devices.Client 1.42.2n Microsoft.Azure.Devices 1.39.1

## 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](https://learn.microsoft.com/en-us/azure/iot-hub/troubleshoot-error-codes#404103-device-not-online)

```(json)
{"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

```(c#)
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

Contributor guide

Open the contributing guide

Research direction

Start with iothub/service/src/Common/Exceptions/ErrorCode.cs, which the issue identifies as missing the 404103 DeviceOffline value. Check how the JSON error code is mapped to DeviceNotFoundException.Code. Done means a 404103 response reports DeviceOffline instead of InvalidErrorCode; the payload does not name a test file to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.