Guide chapters
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. The two types of error pages that can be customized are:
- 404 Not Found
- 500 Internal Server Error
Custom error handlers
Let’s implement custom error handlers in your project:
use ;
use ;
use ;
;
;
;
Create 404.html
:
404 Not Found
404
Page Not Found
Oopsies! The page you're looking for doesn't exist.
Return to Homepage
Create 500.html
:
500 Server Error
500
Server Error
Oopsies! Something went wrong on our end. Please try again later.
Return to Homepage
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:
async
Note that any messages that you pass to the Error
structure will only be displayed in debug mode. In production, the user will see your custom error pages that do not have access to the error message.
Handling specific errors
You can handle specific errors in your views:
async
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 be able to handle specific errors.
Next chapter, we’ll explore automatic testing in Cot applications.