Skip to content
Open
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
22 changes: 20 additions & 2 deletions cpp/src/arrow/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "arrow/status.h"

#include <cassert>
#include <cctype>
#include <cstdlib>
#include <iostream>
#ifdef ARROW_EXTRA_ERROR_CONTEXT
Expand Down Expand Up @@ -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<unsigned char>(message[pos]))) {
break;
}
// Skip all digits
while (pos < message.size() &&
std::isdigit(static_cast<unsigned char>(message[pos]))) {
pos++;
}
// Check if followed by a space
if (pos >= message.size() || message[pos] != ' ') {
break;
}
message = message.substr(0, last_new_line_position);
Expand Down
17 changes: 17 additions & 0 deletions cpp/src/arrow/status_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading