Skip to content

Commit 7e589b6

Browse files
committed
docs: improve error handling page
1 parent 6423d2d commit 7e589b6

2 files changed

Lines changed: 49 additions & 37 deletions

File tree

-10.6 KB
Loading

docs/versions/v3/guides/error-handling.md

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,56 +82,47 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
8282

8383
:::
8484

85-
You don't need to crash the program in your handlers because Textwire will render error page for users. You can read how to customize error pages [here](/v3/guides/error-handling#custom-error-pages).
86-
87-
## Error Pages
88-
89-
### Error Behavior
90-
91-
- **Single File or String Evaluation**: Errors result in empty output
92-
- **Template System**: Errors render a custom error page instead of content
93-
94-
The error page is fully customizable and configurable via the [configuration](/v3/guides/configurations).
85+
:::danger Don't Double-Handle Errors
86+
When `tpl.Response()` returns an error, Textwire has already rendered an error page. Don't call `http.Error()` as it will inject plain text and corrupt the HTML. Just log the error:
9587

96-
:::info Security Considerations
97-
When errors occur, preventing output display protects against incorrect data being shown to users. This maintains data integrity and security. Read more in the [FAQ section](/v3/faq#prevent-visitors-from-seeing-error).
88+
```go
89+
if err := tpl.Response(w, "views/home", data); err != nil {
90+
log.Printf("Template error: %v", err) // [!code highlight]
91+
}
92+
```
9893
:::
9994

100-
### Error in Production
101-
102-
When something goes wrong with your Textwire code, you'll get pre-defined HTML with the static message displayed. This is what people would see when your app is in production:
95+
You don't need to crash the program in your handlers because Textwire will render error page for users. You can read how to customize error pages [here](/v3/guides/error-handling#custom-error-pages).
10396

104-
![Production error page in Textwire](/images/oops.png)
97+
## Error Pages
10598

106-
### Error with Debug Mode
99+
### Debug Mode (Development)
107100

108-
When you enable the `DebugMode` in Textwire, you can see the error message in the browser. This is useful when you are developing your application and want to see the error message in the browser. This is what you would see when the `DebugMode` is set to `true`:
101+
When you enable `DebugMode` in Textwire, detailed error messages display in the browser. This helps during development:
109102

110103
![Debug mode error page in Textwire](/images/debug-error-page.png)
111104

112-
### Custom Error Pages
105+
### Production Errors
113106

114-
Create custom error pages by setting the `ErrorPagePath` configuration. Read more in the [Available Configurations](/v3/guides/configurations#available-configurations) section.
107+
When `DebugMode` is `false`, Textwire shows user-friendly error pages instead of detailed error messages.
115108

116-
#### Creating a Custom Error Page
109+
#### Default Behavior
117110

118-
Use layouts and Textwire syntax for your error page:
111+
Without custom configuration, Textwire displays a pre-defined static HTML page:
119112

120-
```textwire
121-
@use('~main')
113+
![Production error page in Textwire](/images/oops.png)
122114

123-
@insert('title', 'About Us')
115+
#### Custom Error Pages
124116

125-
@insert('content')
126-
<h1>Oops!</h1>
127-
<p>Something went wrong.</p>
128-
<p><a href="/">Go back to home</a></p>
129-
@end
130-
```
117+
You can replace the default error page with your own design.
131118

132-
Save this file in your `templates` directory, preferably as `error-page.tw` or any other name you prefer.
119+
:::warning Debug Mode Only
120+
Custom error pages only appear when `DebugMode` is `false`. Debug mode always shows detailed error information. Don't forget to disable debug mode in production.
121+
:::
133122

134-
#### Configuration Example
123+
##### Configuration
124+
125+
Set `ErrorPagePath` in your configuration:
135126

136127
```go
137128
import (
@@ -148,16 +139,37 @@ func main() {
148139
}
149140
```
150141

151-
With default `TemplateDir` of `"templates"`, the error page will be loaded from `templates/error-page.tw`. If your `TemplateDir` is set to something like `src`, then Textwire will search for `src/error-page.tw`.
142+
With default `TemplateDir` of `"templates"`, the error page loads from `templates/error-page.tw`. If your `TemplateDir` is set to something like `src`, Textwire searches for `src/error-page.tw`.
143+
144+
##### Creating a Custom Error Page
145+
146+
Use layouts and Textwire syntax. You can access [global data](/v3/guides/configurations#global-data) variables:
147+
148+
```textwire
149+
@use('~main')
150+
151+
@insert('title', 'Error')
152+
153+
@insert('content')
154+
<h1>Oops!</h1>
155+
<p>Something went wrong.</p>
156+
<p>&copy; {{ global.year }} My Company</p>
157+
<p><a href="/">Go back to home</a></p>
158+
@end
159+
```
160+
161+
Save this file in your templates directory (e.g., `templates/error-page.tw`).
152162

153-
:::warning Debug Mode Behavior
154-
Custom error pages render only when `DebugMode` is `false`. Debug mode shows detailed error information instead. Don't forget to set `DebugMode` to `false` in production.
163+
:::info Security Considerations
164+
Hiding detailed errors in production protects against information disclosure. This maintains data integrity and security. Read more in the [FAQ section](/v3/faq#prevent-visitors-from-seeing-error).
155165
:::
156166

167+
If the custom error page is missing or has errors, Textwire falls back to the default production error page.
168+
157169
## Best Practices
158170

159171
1. **Always check errors** from Textwire functions
160172
2. **Log errors appropriately** for debugging and monitoring
161173
3. **Use custom error pages** in production for better user experience
162174
4. **Enable debug mode** only during development with `os.Getenv("APP_DEBUG")` or something similar
163-
5. **Handle web errors gracefully** without exposing internal details
175+
5. **Handle web errors gracefully** without exposing internal details to users

0 commit comments

Comments
 (0)