Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <boost/filesystem.hpp>
#include <boost/process.hpp>
#include <gmock/gmock.h>
#include <nlohmann/json.hpp>
#include <chrono>
#include <string>

Expand Down Expand Up @@ -138,8 +139,12 @@ TEST(RunIntegrationTest, ExceptionLogsToStderr) {

std::string line;
std::getline(child_stderr, line);
EXPECT_THAT(line, HasSubstr("standard C++ exception"));
EXPECT_THAT(line, HasSubstr("/exception/test-string"));
auto log = nlohmann::json::parse(line, /*cb=*/nullptr,
/*allow_exceptions=*/false);
ASSERT_TRUE(log.is_object());
EXPECT_EQ(log.value("severity", ""), "error");
EXPECT_THAT(log.value("message", ""), HasSubstr("standard C++ exception"));
EXPECT_THAT(log.value("message", ""), HasSubstr("/exception/test-string"));

try {
(void)HttpGet("localhost", "8010", "/quit/program/0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ TEST(RunIntegrationTest, ExceptionLogsToStderr) {

std::string line;
std::getline(child_stderr, line);
EXPECT_THAT(line, HasSubstr("standard C++ exception"));
EXPECT_THAT(line, HasSubstr("/exception/test-string"));
auto log = nlohmann::json::parse(line, /*cb=*/nullptr,
/*allow_exceptions=*/false);
ASSERT_TRUE(log.is_object());
EXPECT_EQ(log.value("severity", ""), "error");
EXPECT_THAT(log.value("message", ""), HasSubstr("standard C++ exception"));
EXPECT_THAT(log.value("message", ""), HasSubstr("/exception/test-string"));

try {
(void)HttpPost("localhost", "8020", "/quit/program/0");
Expand Down
51 changes: 35 additions & 16 deletions google/cloud/functions/internal/call_user_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "google/cloud/functions/internal/parse_cloud_event_http.h"
#include "google/cloud/functions/internal/wrap_request.h"
#include "google/cloud/functions/internal/wrap_response.h"
#include <nlohmann/json.hpp>
#include <iostream>
#include <stdexcept>

Expand All @@ -32,6 +33,36 @@ struct UnwrapResponse {
}
};

namespace {
BeastResponse ApplicationError(nlohmann::json const& error) {
auto msg = error.dump();
// Log the message to stderr. If the message is properly formatted, as it is
// done here, they are sent picked up and parsed by Cloud Logging:
// https://cloud.google.com/functions/docs/monitoring/logging#writing_structured_logs
std::cerr << msg << std::endl;
Comment thread
coryan marked this conversation as resolved.
BeastResponse response;
response.result(be::http::status::internal_server_error);
response.insert("content-type", "application/json");
response.body() = std::move(msg);
return response;
}

BeastResponse ReportExceptionInFunction(std::exception const& ex) {
return ApplicationError(
{{"severity", "error"},
{"message",
std::string("standard C++ exception thrown by the function: ") +
ex.what()}});
}

BeastResponse ReportUnknownExceptionInFunction() {
return ApplicationError({
{"severity", "error"},
{"message", std::string("unknown C++ exception thrown by the function")},
});
}
} // namespace

BeastResponse CallUserFunction(functions::UserHttpFunction const& function,
BeastRequest request) try {
if (request.target() == "/favicon.ico" || request.target() == "/robots.txt") {
Expand All @@ -42,15 +73,9 @@ BeastResponse CallUserFunction(functions::UserHttpFunction const& function,
auto response = function(MakeHttpRequest(std::move(request)));
return UnwrapResponse::unwrap(std::move(response));
} catch (std::exception const& ex) {
std::cerr << "standard C++ exception thrown: " << ex.what() << std::endl;
BeastResponse response;
response.result(be::http::status::internal_server_error);
return response;
return ReportExceptionInFunction(ex);
} catch (...) {
std::cerr << "unknown c++ exception thrown" << std::endl;
BeastResponse response;
response.result(be::http::status::internal_server_error);
return response;
return ReportUnknownExceptionInFunction();
}

BeastResponse CallUserFunction(
Expand All @@ -67,15 +92,9 @@ BeastResponse CallUserFunction(
}
return BeastResponse{};
} catch (std::exception const& ex) {
std::cerr << "standard C++ exception thrown: " << ex.what() << std::endl;
BeastResponse response;
response.result(be::http::status::internal_server_error);
return response;
return ReportExceptionInFunction(ex);
} catch (...) {
std::cerr << "unknown c++ exception thrown" << std::endl;
BeastResponse response;
response.result(be::http::status::internal_server_error);
return response;
return ReportUnknownExceptionInFunction();
}

} // namespace FUNCTIONS_FRAMEWORK_CPP_NS
Expand Down