From 5ecfdd76ef25f069cd84fac0b0fb3b95e2d61a34 Mon Sep 17 00:00:00 2001 From: Jakob Buchgraber Date: Fri, 26 Jan 2024 01:09:40 -0800 Subject: [PATCH] Improve error message in the crate mapping code. Before this change we would just crash without further details if the crate mapping file or crate mapping was not found. PiperOrigin-RevId: 601694688 --- src/google/protobuf/compiler/rust/context.h | 9 ++++++++- src/google/protobuf/compiler/rust/crate_mapping.cc | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/compiler/rust/context.h b/src/google/protobuf/compiler/rust/context.h index 64df063984..8b17c655ce 100644 --- a/src/google/protobuf/compiler/rust/context.h +++ b/src/google/protobuf/compiler/rust/context.h @@ -13,6 +13,7 @@ #include #include "absl/container/flat_hash_map.h" +#include "absl/log/absl_log.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "absl/types/span.h" @@ -69,7 +70,13 @@ class RustGeneratorContext { } absl::string_view ImportPathToCrateName(absl::string_view import_path) const { - return import_path_to_crate_name_.at(import_path); + auto it = import_path_to_crate_name_.find(import_path); + if (it == import_path_to_crate_name_.end()) { + ABSL_LOG(FATAL) << "Path " << import_path + << " not found in crate mapping. Crate mapping has " + << import_path_to_crate_name_.size() << " entries"; + } + return it->second; } private: diff --git a/src/google/protobuf/compiler/rust/crate_mapping.cc b/src/google/protobuf/compiler/rust/crate_mapping.cc index b8e319021d..ebea7dc263 100644 --- a/src/google/protobuf/compiler/rust/crate_mapping.cc +++ b/src/google/protobuf/compiler/rust/crate_mapping.cc @@ -12,6 +12,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/numbers.h" +#include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" #include "google/protobuf/compiler/rust/context.h"