Error Handling
🧯 Error Handling
Section titled “🧯 Error Handling”Zuno gives you full control over how errors are handled—whether it’s a missing route, a failed validation, or an unexpected exception. You can send custom responses, set status codes, and define global error middleware.
❌ Sending Error Responses
Section titled “❌ Sending Error Responses”You can send an error response from any route handler using res.status()
and res.send()
:
app.get("/protected", [](auto& req, auto& res) {if (!req.headers.contains("Authorization")) { res.status(401).send("Unauthorized"); return;}
res.send("Welcome!");});
🔍 Handling Not Found (404)
Section titled “🔍 Handling Not Found (404)”Zuno automatically returns a 404 if no route matches the request. You can override this with a fallback handler:
app.use([](auto& req, auto& res, auto next) {res.status(404).send("Route not found");});
🧱 Global Error Middleware
Section titled “🧱 Global Error Middleware”You can define a middleware to catch and handle errors across your app. This is useful for logging, masking internal errors, or returning consistent error formats.
Middleware errorHandler = [](auto& req, auto& res, auto next) {try { next(); // Run the next middleware or route} catch (const std::exception& ex) { res.status(500).send("Internal Server Error"); std::cerr << "Unhandled error: " << ex.what() << std::endl;}};
app.use(errorHandler);
🧪 Custom Error Pages
Section titled “🧪 Custom Error Pages”You can return HTML or JSON depending on the client:
app.use([](auto& req, auto& res, auto next) {res.status(404).json({{"error","Not Found"}});});
Or return structured JSON for APIs:
res.status(400).json({{"error", "Invalid input"}{"code", 400}});
🧭 Best Practices
Section titled “🧭 Best Practices”- Always return meaningful status codes (e.g. 400, 401, 404, 500)
- Avoid exposing internal error messages in production
- Use global error middleware to centralize logging and response formatting
- Provide consistent error structures for frontend or API clients
🧭 Next Steps
Section titled “🧭 Next Steps”Now that you can handle errors gracefully, explore:
Zuno helps you fail gracefully—because great DX includes great error handling.