tlbmc: Use non-throwing filesystem APIs in entity_config_json_impl

Google-Bug-Id:478925741
PiperOrigin-RevId: 982116664
Change-Id: I021e495fe4f2ce931bf9308e4caf9e39488f1950
diff --git a/tlbmc/configs/entity_config_json_impl.cc b/tlbmc/configs/entity_config_json_impl.cc
index 34d4a12..143e838 100644
--- a/tlbmc/configs/entity_config_json_impl.cc
+++ b/tlbmc/configs/entity_config_json_impl.cc
@@ -14,6 +14,7 @@
 #include <queue>
 #include <string>
 #include <string_view>
+#include <system_error>  // NOLINT
 #include <tuple>
 #include <utility>
 #include <vector>
@@ -5810,12 +5811,21 @@
     absl::string_view config_location) {
   DLOG(INFO) << "config_location: " << config_location;
   std::vector<nlohmann::json> config_list;
-  if (!std::filesystem::exists(config_location)) {
+  std::error_code ec;
+  if (!std::filesystem::exists(config_location, ec)) {
+    if (ec) {
+      return absl::InternalError(
+          absl::StrCat("Failed to check config directory: ", config_location,
+                       ": ", ec.message()));
+    }
     return config_list;
   }
-  for (const auto& entry :
-       std::filesystem::directory_iterator(config_location)) {
-    if (entry.is_regular_file() && entry.path().extension() == ".json") {
+  for (auto it = std::filesystem::directory_iterator(config_location, ec);
+       !ec && it != std::filesystem::directory_iterator(); it.increment(ec)) {
+    const auto& entry = *it;
+    std::error_code entry_ec;
+    if (entry.is_regular_file(entry_ec) &&
+        entry.path().extension() == ".json") {
       std::string json_file_path = entry.path().string();
       std::ifstream json_file(json_file_path);
       if (!json_file.is_open()) {
@@ -5841,8 +5851,16 @@
         DLOG(INFO) << "parsed config: " << json_file_path;
         config_list.push_back(config);
       }
+    } else if (entry_ec) {
+      LOG(WARNING) << "Skipping config entry '" << entry.path().string()
+                   << "': " << entry_ec.message();
     }
   }
+  if (ec) {
+    return absl::InternalError(
+        absl::StrCat("Failed to iterate config directory: ", config_location,
+                     ": ", ec.message()));
+  }
   return config_list;
 }