diff --git a/cpp/src/arrow/status.cc b/cpp/src/arrow/status.cc index 55ce3fb78d2..4730bca8c6c 100644 --- a/cpp/src/arrow/status.cc +++ b/cpp/src/arrow/status.cc @@ -13,6 +13,7 @@ #include "arrow/status.h" #include +#include #include #include #ifdef ARROW_EXTRA_ERROR_CONTEXT @@ -131,8 +132,25 @@ std::string Status::ToStringWithoutContextLines() const { if (last_new_line_position == std::string::npos) { break; } - // TODO: We may want to check /:\d+ / - if (message.find(":", last_new_line_position) == std::string::npos) { + // Check for the pattern ":\d+ " (colon followed by one or more digits and a space) + // to identify context lines in the format "filename:line expr" + auto colon_position = message.find(":", last_new_line_position); + if (colon_position == std::string::npos) { + break; + } + // Verify that the colon is followed by one or more digits and then a space + size_t pos = colon_position + 1; + if (pos >= message.size() || + !std::isdigit(static_cast(message[pos]))) { + break; + } + // Skip all digits + while (pos < message.size() && + std::isdigit(static_cast(message[pos]))) { + pos++; + } + // Check if followed by a space + if (pos >= message.size() || message[pos] != ' ') { break; } message = message.substr(0, last_new_line_position); diff --git a/cpp/src/arrow/status_test.cc b/cpp/src/arrow/status_test.cc index 39a52bd2bad..72998cba78f 100644 --- a/cpp/src/arrow/status_test.cc +++ b/cpp/src/arrow/status_test.cc @@ -342,4 +342,21 @@ TEST(StatusTest, ReturnIfNotOk) { ASSERT_EQ(StripContext(st.message()), "StatusLike: 43"); } +#ifdef ARROW_EXTRA_ERROR_CONTEXT +TEST(StatusTest, ToStringWithoutContextLines) { + Status status = Status::IOError("base error"); + status.AddContextLine("file1.cc", 42, "expr"); + status.AddContextLine("file2.cc", 100, "expr"); + + ASSERT_EQ(status.ToStringWithoutContextLines(), "IOError: base error"); + + Status status2(StatusCode::Invalid, + "Error message\nThis line has: a colon but no digits"); + status2.AddContextLine("file.cc", 20, "expr"); + + ASSERT_EQ(status2.ToStringWithoutContextLines(), + "Invalid: Error message\nThis line has: a colon but no digits"); +} +#endif + } // namespace arrow