mirror of
https://github.com/pytorch/pytorch.git
synced 2025-11-02 14:34:54 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/37101 Fixes #36954. The basic concept is to streamline the process of rethrowing c10::Error with extra error information. This is in a few steps: - I completely remodeled the Error data type and the internal invariants. Instead of manually adding in newlines, the message stack formatting process is responsible for inserting newlines and spacing as necessary. Call sites are then modified to respect the new API model. - TORCH_RETHROW macro is added, which adds context to an error message and then rethrows it. New internal assert failure looks like: ``` 0 INTERNAL ASSERT FAILED at ../c10/test/util/exception_test.cpp:64, please report a bug to PyTorch. Exception raised from TestBody at ../c10/test/util/exception_test.cpp:64 (most recent call first): frame #0: <unknown function> + 0x6aab9 (0x7ff611d3aab9 in /data/users/ezyang/pytorch-tmp/build/lib/libc10.so) frame #1: ... ``` Error message with context looks like: ``` This is an error This is context 1 This is context 2 ``` Signed-off-by: Edward Z. Yang <ezyang@fb.com> Test Plan: Imported from OSS Differential Revision: D21202891 Pulled By: ezyang fbshipit-source-id: 361cadd16bc52e5886dba08e79277771ada76169
68 lines
1.7 KiB
C++
68 lines
1.7 KiB
C++
#include <c10/util/Exception.h>
|
|
#include <gtest/gtest.h>
|
|
#include <stdexcept>
|
|
|
|
using c10::Error;
|
|
|
|
namespace {
|
|
bool throw_func() {
|
|
throw std::runtime_error("I'm throwing...");
|
|
}
|
|
|
|
template<class Functor>
|
|
inline void expectThrowsEq(Functor&& functor, const char* expectedMessage) {
|
|
try {
|
|
std::forward<Functor>(functor)();
|
|
} catch (const Error& e) {
|
|
EXPECT_STREQ(e.what_without_backtrace(), expectedMessage);
|
|
return;
|
|
}
|
|
ADD_FAILURE() << "Expected to throw exception with message \""
|
|
<< expectedMessage << "\" but didn't throw";
|
|
}
|
|
} // namespace
|
|
|
|
TEST(ExceptionTest, TORCH_INTERNAL_ASSERT_DEBUG_ONLY) {
|
|
#ifdef NDEBUG
|
|
ASSERT_NO_THROW(TORCH_INTERNAL_ASSERT_DEBUG_ONLY(false));
|
|
// Does nothing - `throw_func()` should not be evaluated
|
|
ASSERT_NO_THROW(TORCH_INTERNAL_ASSERT_DEBUG_ONLY(throw_func()));
|
|
#else
|
|
ASSERT_THROW(TORCH_INTERNAL_ASSERT_DEBUG_ONLY(false), c10::Error);
|
|
ASSERT_NO_THROW(TORCH_INTERNAL_ASSERT_DEBUG_ONLY(true));
|
|
#endif
|
|
}
|
|
|
|
TEST(WarningTest, JustPrintWarning) {
|
|
TORCH_WARN("I'm a warning");
|
|
}
|
|
|
|
TEST(ExceptionTest, ErrorFormatting) {
|
|
expectThrowsEq([]() {
|
|
TORCH_CHECK(false, "This is invalid");
|
|
}, "This is invalid");
|
|
|
|
expectThrowsEq([]() {
|
|
try {
|
|
TORCH_CHECK(false, "This is invalid");
|
|
} catch (Error& e) {
|
|
TORCH_RETHROW(e, "While checking X");
|
|
}
|
|
}, "This is invalid (While checking X)");
|
|
|
|
expectThrowsEq([]() {
|
|
try {
|
|
try {
|
|
TORCH_CHECK(false, "This is invalid");
|
|
} catch (Error& e) {
|
|
TORCH_RETHROW(e, "While checking X");
|
|
}
|
|
} catch (Error& e) {
|
|
TORCH_RETHROW(e, "While checking Y");
|
|
}
|
|
},
|
|
R"msg(This is invalid
|
|
While checking X
|
|
While checking Y)msg");
|
|
}
|