NVMeBasicContext: Catch FileHandle out-of-range exception

In execBasicQuery, when the i2c bus path of the nvme sensor
does not exist, it still does file handling and causes FileHandle
class to throw out-of-range exception and terminate the service.
This leads to coredump for the system which is undesirable.

This commit adds a try catch to this action of handle i2c bus path,
which will log an error message and return when it fails to open
the i2c bus path.

Signed-off-by: chaul.ampere <chaul@amperecomputing.com>
Change-Id: Ice1d363a6149607bd149b89f70f2a23812df999d
diff --git a/src/NVMeBasicContext.cpp b/src/NVMeBasicContext.cpp
index 322c133..465f177 100644
--- a/src/NVMeBasicContext.cpp
+++ b/src/NVMeBasicContext.cpp
@@ -63,40 +63,51 @@
 {
     int32_t size = 0;
     std::filesystem::path devpath = "/dev/i2c-" + std::to_string(bus);
-    FileHandle fileHandle(devpath);
 
-    /* Select the target device */
-    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
-    if (::ioctl(fileHandle.handle(), I2C_SLAVE, addr) == -1)
+    try
     {
-        std::cerr << "Failed to configure device address 0x" << std::hex
-                  << (int)addr << " for bus " << std::dec << bus << ": "
-                  << strerror(errno) << "\n";
-        return;
+        FileHandle fileHandle(devpath);
+
+        /* Select the target device */
+        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+        if (::ioctl(fileHandle.handle(), I2C_SLAVE, addr) == -1)
+        {
+            std::cerr << "Failed to configure device address 0x" << std::hex
+                      << (int)addr << " for bus " << std::dec << bus << ": "
+                      << strerror(errno) << "\n";
+            resp.resize(0);
+            return;
+        }
+
+        resp.resize(UINT8_MAX + 1);
+
+        /* Issue the NVMe MI basic command */
+        size = i2c_smbus_read_block_data(fileHandle.handle(), cmd, resp.data());
+        if (size < 0)
+        {
+            std::cerr << "Failed to read block data from device 0x" << std::hex
+                      << (int)addr << " on bus " << std::dec << bus << ": "
+                      << strerror(errno) << "\n";
+            resp.resize(0);
+        }
+        else if (size > UINT8_MAX + 1)
+        {
+            std::cerr << "Unexpected message length from device 0x" << std::hex
+                      << (int)addr << " on bus " << std::dec << bus << ": "
+                      << size << " (" << UINT8_MAX << ")\n";
+            resp.resize(0);
+        }
+        else
+        {
+            resp.resize(size);
+        }
     }
-
-    resp.resize(UINT8_MAX + 1);
-
-    /* Issue the NVMe MI basic command */
-    size = i2c_smbus_read_block_data(fileHandle.handle(), cmd, resp.data());
-    if (size < 0)
+    catch (const std::out_of_range& e)
     {
-        std::cerr << "Failed to read block data from device 0x" << std::hex
-                  << (int)addr << " on bus " << std::dec << bus << ": "
-                  << strerror(errno) << "\n";
+        std::cerr << "Failed to create file handle for bus " << std::dec << bus
+                  << ": " << e.what() << "\n";
         resp.resize(0);
     }
-    else if (size > UINT8_MAX + 1)
-    {
-        std::cerr << "Unexpected message length from device 0x" << std::hex
-                  << (int)addr << " on bus " << std::dec << bus << ": " << size
-                  << " (" << UINT8_MAX << ")\n";
-        resp.resize(0);
-    }
-    else
-    {
-        resp.resize(size);
-    }
 }
 
 static ssize_t processBasicQueryStream(FileHandle& in, FileHandle& out)