Skip to content

Commit f89425a

Browse files
Report the value that failed to cast in safe_cast error message (#1111)
rapidsai/cudf#22964 is a cudf-polars issue with a traceback pointing to rapidsmpf's safe_cast. To help with debugging that, we'll include the value that failed to cast in the error message. Authors: - Tom Augspurger (https://github.com/TomAugspurger) Approvers: - Peter Andreas Entschev (https://github.com/pentschev) - Niranda Perera (https://github.com/nirandaperera) URL: #1111
1 parent 649545d commit f89425a

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

cpp/include/rapidsmpf/utils/misc.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ constexpr To safe_cast(
401401
if (!std::in_range<To>(value)) {
402402
throw std::overflow_error(
403403
"RapidsMPF cast error at: " + std::string(loc.file_name()) + ":"
404-
+ std::to_string(loc.line()) + ", value out of range"
404+
+ std::to_string(loc.line())
405+
+ ", value out of range (value=" + std::to_string(value) + ")"
405406
);
406407
}
407408
return static_cast<To>(value);

cpp/tests/test_misc.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <cstdint>
77
#include <limits>
8+
#include <string>
89

910
#include <gtest/gtest.h>
1011

@@ -199,7 +200,7 @@ TEST(MiscTest, SafeCastFloatingPointConversions) {
199200
EXPECT_EQ(safe_cast<int>(-2.7), -2);
200201
}
201202

202-
// Test that error messages include source location
203+
// Test that error messages include source location & value.
203204
TEST(MiscTest, SafeCastErrorMessageContainsLocation) {
204205
try {
205206
safe_cast<unsigned>(-1);
@@ -211,9 +212,24 @@ TEST(MiscTest, SafeCastErrorMessageContainsLocation) {
211212
<< "Error message should contain file name: " << msg;
212213
EXPECT_TRUE(msg.find("RapidsMPF cast error") != std::string::npos)
213214
<< "Error message should contain 'RapidsMPF cast error': " << msg;
215+
EXPECT_TRUE(msg.find("value=-1") != std::string::npos)
216+
<< "Error message should contain 'value=-1': " << msg;
214217
}
215218
}
216219

220+
// Test that error messages include large unsigned source values.
221+
TEST(MiscTest, SafeCastErrorMessageContainsLargeUnsignedValue) {
222+
std::string msg;
223+
try {
224+
safe_cast<std::int64_t>(UINT64_MAX);
225+
FAIL() << "Expected std::overflow_error";
226+
} catch (const std::overflow_error& e) {
227+
msg = e.what();
228+
}
229+
EXPECT_TRUE(msg.find("value=" + std::to_string(UINT64_MAX)) != std::string::npos)
230+
<< "Error message should contain large unsigned value: " << msg;
231+
}
232+
217233
// Test zero and boundary values
218234
TEST(MiscTest, SafeCastZeroAndBoundaries) {
219235
// Zero should always work

0 commit comments

Comments
 (0)