Skip to content

Commit b146449

Browse files
committed
feat: improve error message for CloudEvent
When parsing CloudEvents in JSON format the error message was a bit obscure, it indicated that a field was missing, but not which one. This is more readable in the common case.
1 parent bf2ba9a commit b146449

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

google/cloud/functions/internal/parse_cloud_event_json.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ inline namespace FUNCTIONS_FRAMEWORK_CPP_NS {
2323
namespace {
2424

2525
functions::CloudEvent ParseCloudEventJson(nlohmann::json const& json) {
26+
if (!json.contains("id") || !json.contains("source") ||
27+
!json.contains("type")) {
28+
throw std::runtime_error(
29+
"JSON message missing `id`, `source`, and/or `type` fields");
30+
}
31+
2632
auto event = functions::CloudEvent(
2733
json.at("id").get<std::string>(), json.at("source").get<std::string>(),
2834
json.at("type").get<std::string>(),

0 commit comments

Comments
 (0)