Stiffstream / restinio

Cross-platform, efficient, customizable, and robust asynchronous HTTP(S)/WebSocket server C++ library with the right balance between performance and ease of use
Other
1.15k stars 93 forks source link

How to debug the handlers? #146

Open zhangzq opened 3 years ago

zhangzq commented 3 years ago

My app showed me an error message:

ERROR: [connection:5] error while handling request: basic_string::_M_replace_aux

I didn't know where the error cames from (even I compiled in debug mode and run in gdb). What should I do?

eao197 commented 3 years ago

Hi!

Looks like you get a std::length_error somewhere in your request-handler. That exception is caught by RESTinio and logger.

It's hard to help you because I don't know anything about your code. You can try to place the code in your request-handler into try-catch block:

auto my_request_handler(const restinio::request_handle_t & req) {
  try {
    ... // Your code here.
  }
  catch(const std::exception & x) {
    std::cerr << "Oops! " << x.what() << std::endl; // (1)
  }
}

Then set a breakpoint at (1).

zhangzq commented 3 years ago

Is there an MACRO -- not to catch the exception and let the app core dump, then let me check the error stack (in gdb)?

eao197 commented 3 years ago

No, there is no such a macro.

You can comment try/catch blocks here: https://github.com/Stiffstream/restinio/blob/0052518f5692f8f051031e06d933b726191be97e/dev/restinio/impl/connection.hpp#L741-L749

zhangzq commented 3 years ago

Hi!

Looks like you get a std::length_error somewhere in your request-handler. That exception is caught by RESTinio and logger.

It's hard to help you because I don't know anything about your code. You can try to place the code in your request-handler into try-catch block:

auto my_request_handler(const restinio::request_handle_t & req) {
  try {
    ... // Your code here.
  }
  catch(const std::exception & x) {
    std::cerr << "Oops! " << x.what() << std::endl; // (1)
  }
}

Then set a breakpoint at (1).

Breakpoint at (1) won't provide the error stack.

zhangzq commented 3 years ago

No, there is no such a macro.

You can comment try/catch blocks here:

https://github.com/Stiffstream/restinio/blob/0052518f5692f8f051031e06d933b726191be97e/dev/restinio/impl/connection.hpp#L741-L749

Thanks. I comment/uncomment using #ifdef DBUG, but get catched by some other place and show error messsage:

unexpected exception during the handling of incoming data: basic_string::_M_replace_aux
eao197 commented 3 years ago

Breakpoint at (1) won't provide the error stack.

Oops. Ok. Let's try something like that:

class dummy_breakpoint {
  bool commited_{false};
public:
  dummy_breakpoint() = default;
  ~dummy_breakpoint() { if(!commited_) std::abort(); } // or just throw std::runtime_error("my exception");

  void commit() { commited_ = true; }
};

// Then in your request-handler:
auto my_request_handler(const restinio::request_handle_t & req) {
  dummy_breakpoint breakpoint;
  ...
  const auto res = req->create_response(...)...;
  dummy_breakpoint.commit();
  return res;
}