You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/versions/v3/guides/error-handling.md
+49-37Lines changed: 49 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,56 +82,47 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
82
82
83
83
:::
84
84
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:
95
87
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).
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).
103
96
104
-

97
+
## Error Pages
105
98
106
-
### Error with Debug Mode
99
+
### Debug Mode (Development)
107
100
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:
109
102
110
103

111
104
112
-
### Custom Error Pages
105
+
### Production Errors
113
106
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.
115
108
116
-
#### Creating a Custom Error Page
109
+
#### Default Behavior
117
110
118
-
Use layouts and Textwire syntax for your error page:
111
+
Without custom configuration, Textwire displays a pre-defined static HTML page:
119
112
120
-
```textwire
121
-
@use('~main')
113
+

122
114
123
-
@insert('title', 'About Us')
115
+
#### Custom Error Pages
124
116
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.
131
118
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
+
:::
133
122
134
-
#### Configuration Example
123
+
##### Configuration
124
+
125
+
Set `ErrorPagePath` in your configuration:
135
126
136
127
```go
137
128
import (
@@ -148,16 +139,37 @@ func main() {
148
139
}
149
140
```
150
141
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:
Save this file in your templates directory (e.g., `templates/error-page.tw`).
152
162
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).
155
165
:::
156
166
167
+
If the custom error page is missing or has errors, Textwire falls back to the default production error page.
168
+
157
169
## Best Practices
158
170
159
171
1.**Always check errors** from Textwire functions
160
172
2.**Log errors appropriately** for debugging and monitoring
161
173
3.**Use custom error pages** in production for better user experience
162
174
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