Error pages
Error pages in Cot provide users with helpful information when something goes wrong. Let’s learn how to handle errors gracefully and create custom error pages.
Debug mode error pages
In development (debug mode), Cot provides detailed error pages that include:
- Error message and type
- Stack trace
- Request information
- Configuration details
- Route information
The debug mode is enabled in the default dev configuration:
# config/dev.toml
= true
Now, when you visit a non-existing page, or if your code raises an error or panics, Cot will display a detailed error page with the information useful to debug the issue. Note that the error pages in debug mode may contain sensitive information, so you should always make sure it is disabled in production!
Default error pages
When the debug mode is disabled, Cot provides default error pages that do not share any information about what happened to the user. To match your service’s look and feel, you’ll typically want to customize them.
Custom error handlers
Let’s implement a custom error handler in your project:
use ;
use ;
use ;
use ;
async
;
Create templates/error.html:
Error
{{ error.status_code().as_u16() }}
{{ error.status_code().canonical_reason().unwrap_or("Error") }}
Now, try to visit an undefined route or raise an error in your code. You should see the custom error pages you’ve created!
Raising errors in views
Cot provides several ways to raise errors in your views:
use ;
use ;
use ;
use ;
async
Note that any messages that you pass to the Error structure will only be displayed in debug mode by default. In production, the user will see your custom error pages (which may or may not retrieve the underlying error message, depending on how you implemented them).
Summary
In this chapter, you learned how to handle errors in Cot applications. You can create custom error pages, raise errors in your views, and overall provide a better user experience when something goes wrong.
Next chapter, we’ll explore automatic testing in Cot applications.