ATen icon indicating copy to clipboard operation
ATen copied to clipboard

Put ErrorReporting helper in ATen

Open ezyang opened this issue 6 years ago • 0 comments

struct ErrorReport : public std::exception {
  ErrorReport(const ErrorReport& e)
      : ss(e.ss.str()), context(e.context), the_message(e.the_message) {}

  ErrorReport() : context(nullptr) {}
  ErrorReport(TreeRef context) : context(context) {}

  virtual const char* what() const noexcept override {
    std::stringstream msg;
    msg << "\n" << ss.str();
    if (context != nullptr) {
      msg << ":\n";
      context->range().highlight(msg);
    } else {
      msg << ".\n";
    }
    the_message = msg.str();
    return the_message.c_str();
  }

 private:
  template <typename T>
  friend const ErrorReport& operator<<(const ErrorReport& e, const T& t);

  mutable std::stringstream ss;
  TreeRef context;
  mutable std::string the_message;
};

template <typename T>
const ErrorReport& operator<<(const ErrorReport& e, const T& t) {
  e.ss << t;
  return e;
}

From https://github.com/pytorch/pytorch/pull/3666#discussion_r153818743

ezyang avatar Nov 30 '17 13:11 ezyang