| 374 | } |
| 375 | |
| 376 | void ThrowError(const std::shared_ptr<StackedError>& error) { |
| 377 | std::string error_str; |
| 378 | fmt::format_to(std::back_inserter(error_str), "{}: {}\n", |
| 379 | fmt::styled("Error", fmt::emphasis::bold | fmt::fg(fmt::color::red)), |
| 380 | GetErrorString(error)); |
| 381 | // Append foreign stack trace (e.g. Python stack trace) when it is available. |
| 382 | if (ForeignFrameThreadLocalGuard::Current().has_value()) { |
| 383 | auto frame = *CHECK_JUST(ForeignFrameThreadLocalGuard::Current()); |
| 384 | if (!IsMainThread()) { |
| 385 | if (auto* stack_getter = Singleton<ForeignStackGetter>::Get()) { |
| 386 | fmt::format_to(std::back_inserter(error_str), |
| 387 | fmt::emphasis::bold | fmt::fg(fmt::color::dark_orange), |
| 388 | "Related Python stack trace:"); |
| 389 | if (IsPythonStackGetterEnabledByDebugBuild()) { |
| 390 | fmt::format_to( |
| 391 | std::back_inserter(error_str), |
| 392 | " (You are seeing this stack trace because you compiled OneFlow with " |
| 393 | "CMAKE_BUILD_TYPE=Debug. If you want to see it even with other CMAKE_BUILD_TYPEs, " |
| 394 | "you can set ONEFLOW_DEBUG or ONEFLOW_PYTHON_STACK_GETTER to 1)"); |
| 395 | } |
| 396 | fmt::format_to(std::back_inserter(error_str), "\n{}", |
| 397 | stack_getter->GetFormattedStack(frame)); |
| 398 | } else { |
| 399 | fmt::format_to( |
| 400 | std::back_inserter(error_str), |
| 401 | "You can set {} or {} to 1 to get the Python stack of the error.", |
| 402 | fmt::styled("ONEFLOW_DEBUG", fmt::emphasis::bold | fmt::fg(fmt::color::dark_orange)), |
| 403 | fmt::styled("ONEFLOW_PYTHON_STACK_GETTER", |
| 404 | fmt::emphasis::bold | fmt::fg(fmt::color::dark_orange))); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | *MutThreadLocalError() = error; |
| 409 | if ((*error)->has_runtime_error()) { throw RuntimeException(error_str); } |
| 410 | if ((*error)->has_type_error()) { throw TypeException(error_str); } |
| 411 | if ((*error)->has_index_error()) { throw IndexException(error_str); } |
| 412 | if ((*error)->has_unimplemented_error()) { throw NotImplementedException(error_str); } |
| 413 | throw Exception(GetStackedErrorString(error)); |
| 414 | } |
| 415 | |
| 416 | const std::shared_ptr<StackedError>& ThreadLocalError() { return *MutThreadLocalError(); } |
| 417 |
no test coverage detected