Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler called when error occurred #407

Closed
thall opened this issue Mar 31, 2020 · 3 comments
Closed

Handler called when error occurred #407

thall opened this issue Mar 31, 2020 · 3 comments
Labels

Comments

@thall
Copy link

thall commented Mar 31, 2020

Hi,

I'm a bit confused why my handler is called when a error occurred when parsing the request.

The setup:

// Custom logger
svr.set_logger([](const Request& req, const Response& res) { log(req, res); });

// Custom error handler
svr.set_error_handler([](const Request& req, Response& res) {
  (void)req;
  (void)res;
  log("ERROR OCCURRED");
});

// Endpoint
svr.Post(R"(/odd/(\d+))", [pOddEndpoint](const Request& req, Response& res) {
  int tileId = stoi(req.matches[1]);
  pOddEndpoint->save(tileId, req, res);
});

Output log:

Log(13:48:16): Successfully started
Log(13:48:23): ERROR OCCURRED
Log(13:48:23): 127.0.0.1 - POST /odd/34 [] 400 0
Log(13:48:23): <handler log>
Log(13:48:23): 127.0.0.1 - POST /odd/34 [] 200 0

When sending an empty POST request to /odd/<id> it times out after 5 seconds, and the error_handler is called and the logger, BUT then also my handler is called, why does that happen? I didn't expect my handler to be called if something is wrong with the request.

Is this expected behavior?

Request:

$ time curl -v -X POST http://localhost:8500/odd/34
*   Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 8500 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8500 (#0)
> POST /odd/34 HTTP/1.1
> Host: localhost:8500
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 400 Bad Request
< Content-Length: 0
< 
* Connection #0 to host localhost left intact

real    0m5,024s
user    0m0,001s
sys     0m0,014s
@yhirose
Copy link
Owner

yhirose commented Mar 31, 2020

@thall, thanks for the report. It's apparently a bug. I'll look into it. Thanks!

@yhirose yhirose added the bug label Mar 31, 2020
@yhirose
Copy link
Owner

yhirose commented Mar 31, 2020

@thall, I think I fixed it. Could you try the latest code?

@thall
Copy link
Author

thall commented Apr 1, 2020

@yhirose now it behaves as expected, thanks for quick response!

yhirose added a commit that referenced this issue May 2, 2020
ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this issue May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants