1 #pragma once 2 #include <c10/util/Exception.h> 3 #include <stdexcept> 4 5 namespace torch::jit { 6 7 struct ExceptionMessage { ExceptionMessageExceptionMessage8 ExceptionMessage(const std::exception& e) : e_(e) {} 9 10 private: 11 const std::exception& e_; 12 friend std::ostream& operator<<( 13 std::ostream& out, 14 const ExceptionMessage& msg); 15 }; 16 17 inline std::ostream& operator<<( 18 std::ostream& out, 19 const ExceptionMessage& msg) { 20 auto c10_error = dynamic_cast<const c10::Error*>(&msg.e_); 21 if (c10_error) { 22 out << c10_error->what_without_backtrace(); 23 } else { 24 out << msg.e_.what(); 25 } 26 return out; 27 } 28 29 } // namespace torch::jit 30