Skip to content

Handler called when error occurred #407

@thall

Description

@thall

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions