drogonframework / drogonframework/drogon

Invalid route parameter returns 500 instead of 404

Open
#2,218 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
14.3k
Forks
1.4k
Avg merge
1d 13h
Merged PRs (30d)
14

Description

**Describe the bug**
When making a request to the route /api/num/{number} with a parameter like /api/num/s-1.2e-3, the server returns a 500 Internal Server Error instead of the expected 404 Not Found.
The URL /api/num/s-1.2e-3 should not match the route /api/num/ since it starts with s and is not a valid double. However, the server currently throws a 500 Internal Server Error.
And here is the log:
```
20241201 04:41:45.637000 UTC 37404 ERROR Unhandled exception in /api/num/s-1.2e-3, what(): invalid stod argument - HttpAppFrameworkImpl.cc:124
```

**To Reproduce**
Steps to reproduce the behavior:
1. Set up a Drogon server with the following handler registration:
```
#pragma comment(lib, "Crypt32")
#pragma comment(lib, "Rpcrt4")

#include
#include

int main(int argc, char *argv[])
{
auto &app = drogon::app();

app.registerHandler("/api/num/{number}",
[](const drogon::HttpRequestPtr &, std::function &&callback, double number) {
auto resp = drogon::HttpResponse::newHttpResponse();
std::string text = std::to_string(number);
resp->setBody(text);
resp->setContentTypeCode(drogon::CT_TEXT_PLAIN);
resp->setStatusCode(drogon::k200OK);
callback(resp);
},
{drogon::Get});

app.setLogLevel(trantor::Logger::kInfo)
.setLogPath("./")
.addListener("0.0.0.0", 18080)
.addListener("::0", 18080)
.setThreadNum(6)
.setIdleConnectionTimeout(std::chrono::seconds(5))
.run();

return 0;
}
```

**Expected behavior**
I expected the server to return a 404 Not Found error, as the parameter s-1.2e-3 is not a valid double (it starts with s and cannot be parsed as a number). The server should not attempt to match the route when the parameter is invalid.

**Screenshots**
捕获

**Desktop (please complete the following information):**
- OS: Windows 11
- Compiler: Visual Studio 2022
- C++ Standard: C++17
- Drogon version: 1.9.6
- Charset: Unicode

**Additional context**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.