From b146449db26f051401a10a44dcc2b1aac9e32601 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Thu, 12 Aug 2021 14:09:58 +0000 Subject: [PATCH] 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. --- google/cloud/functions/internal/parse_cloud_event_json.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/google/cloud/functions/internal/parse_cloud_event_json.cc b/google/cloud/functions/internal/parse_cloud_event_json.cc index e1ea8fc0..2f979fd9 100644 --- a/google/cloud/functions/internal/parse_cloud_event_json.cc +++ b/google/cloud/functions/internal/parse_cloud_event_json.cc @@ -23,6 +23,12 @@ inline namespace FUNCTIONS_FRAMEWORK_CPP_NS { namespace { functions::CloudEvent ParseCloudEventJson(nlohmann::json const& json) { + if (!json.contains("id") || !json.contains("source") || + !json.contains("type")) { + throw std::runtime_error( + "JSON message missing `id`, `source`, and/or `type` fields"); + } + auto event = functions::CloudEvent( json.at("id").get(), json.at("source").get(), json.at("type").get(),