mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-14 10:29:35 +00:00
Add exception handler (#845)
* Add exception handler * revert content reader changes * Add test for and fix exception handler * Fix warning in test * Readd exception test, improve readme note, don't rethrow errors, remove exception handler response
This commit is contained in:
1 parent
78c474c744
commit
0542fdb8e4
3 files changed
+90
-23
No files matched your search
@@ -177,6 +177,19 @@ svr.set_error_handler([](const auto& req, auto& res) {
|
||||
});
|
||||
```
|
||||
|
||||
### Exception handler
|
||||
The exception handler gets called if a user routing handler throws an error.
|
||||
|
||||
```cpp
|
||||
svr.set_exception_handler([](const auto& req, auto& res, std::exception &e) {
|
||||
res.status = 500;
|
||||
auto fmt = "<h1>Error 500</h1><p>%s</p>";
|
||||
char buf[BUFSIZ];
|
||||
snprintf(buf, sizeof(buf), fmt, e.what());
|
||||
res.set_content(buf, "text/html");
|
||||
});
|
||||
```
|
||||
|
||||
### Pre routing handler
|
||||
|
||||
```cpp
|
||||
|
||||
Reference in new issue
Block a user