@@ -40,15 +40,22 @@ for many use cases. Restricting path parameters allows us to use memory efficien
40
40
` echo.NotFoundHandler(h Handler) `
41
41
42
42
Registers a custom NotFound handler. This handler is called in case router doesn't
43
- find matching route for the request.
43
+ find a matching route for the HTTP request.
44
44
45
45
Default handler sends 404 "Not Found" response.
46
46
47
47
### HTTP error handler
48
48
49
49
` echo.HTTPErrorHandler(h HTTPErrorHandler) `
50
50
51
- Registers a custom centralized HTTP error handler.
51
+ Registers a custom centralized HTTP error handler ` func(*HTTPError, *Context) ` .
52
+
53
+ Default handler sends ` HTTPError.Message ` HTTP response with ` HTTPError.Code ` status
54
+ code.
55
+
56
+ - If HTTPError.Code is not specified it uses 500 "Internal Server Error".
57
+ - If HTTPError.Message is not specified it uses HTTPError.Error.Error() or the status
58
+ code text.
52
59
53
60
## Routing
54
61
@@ -60,7 +67,7 @@ zero dynamic memory allocation with no GC overhead.
60
67
61
68
Routes can be registered for any HTTP method, path and handler. For example, code
62
69
below registers a route for method ` GET ` , path ` /hello ` and a handler which sends
63
- ` Hello! ` response.
70
+ ` Hello! ` HTTP response.
64
71
65
72
``` go
66
73
echo.Get (" /hello" , func (*echo.Context ) *HTTPError {
@@ -69,14 +76,16 @@ echo.Get("/hello", func(*echo.Context) *HTTPError {
69
76
```
70
77
71
78
Echo's default handler is ` func(*echo.Context) *echo.HTTPError ` where ` echo.Context `
72
- primarily holds request and response objects. Echo also has a support for other
79
+ primarily holds HTTP request and response objects. Echo also has a support for other
73
80
types of handlers.
74
81
75
82
<!-- TODO mention about not able to take advantage -->
76
83
77
- <!-- ### Groups -->
84
+ ### Group
85
+
86
+ ** WIP**
78
87
79
- ### Path parameters
88
+ ### Path parameter
80
89
81
90
URL path parameters can be extracted either by name ` echo.Context.Param(name string) string ` or by
82
91
index ` echo.Context.P(i uint8) string ` . Getting parameter by index gives a slightly
@@ -164,23 +173,23 @@ e.Get("/users/:id", h)
164
173
context.JSON (code int , v interface {}) *HTTPError
165
174
```
166
175
167
- Sends a JSON response with status code.
176
+ Sends a JSON HTTP response with status code.
168
177
169
178
### String
170
179
171
180
``` go
172
181
context.String (code int , s string ) *HTTPError
173
182
```
174
183
175
- Sends a text/plain response with status code.
184
+ Sends a text/plain HTTP response with status code.
176
185
177
186
### HTML
178
187
179
188
``` go
180
189
func (c *Context ) HTML (code int , html string ) *HTTPError
181
190
```
182
191
183
- Sends an HTML response with status code.
192
+ Sends an HTML HTTP response with status code.
184
193
185
194
### Static files
186
195
@@ -210,8 +219,48 @@ index.html for path `/`
210
219
e.Index("index.html")
211
220
```
212
221
213
- <!-- ## Error Handling -->
222
+ ## Error Handling
223
+
224
+ Echo advocates centralized HTTP error handling by returning `*echo.HTTPError` from
225
+ middleware and handlers.
226
+
227
+ - In debug mode, write stack trace to the HTTP response.
228
+ - Customize HTTP responses, may be using a pretty HTML template.
229
+ - Recover from panics inside middleware or handlers.
230
+
231
+ For example, when a basic auth middleware finds invalid credentials it returns 401
232
+ "Unauthorized" error, aborting the current HTTP request.
233
+
234
+ ```go
235
+ package main
236
+
237
+ import (
238
+ "net/http"
239
+
240
+ "github.com/labstack/echo"
241
+ )
242
+
243
+ func main() {
244
+ e := echo.New ()
245
+ e.Use (func (c *echo.Context ) *echo.HTTPError {
246
+ // Extract the credentials from HTTP request header and perform a security
247
+ // check
248
+
249
+ // For invalid credentials
250
+ return &echo.HTTPError {Code: http.StatusUnauthorized }
251
+ })
252
+ e.Get (" /welcome" , welcome)
253
+ e.Run (" :4444" )
254
+ }
255
+
256
+ func welcome (c *echo .Context ) *echo .HTTPError {
257
+ return c.String (http.StatusOK , " Welcome!" )
258
+ }
259
+ ```
260
+
261
+ ` echo.HTTPError ` has status code, message and error fields. Internally it uses
262
+ ` http.Error ` to send HTTP response with status code and message.
214
263
215
- <!-- message not set err.Error() or status text -->
264
+ ## Deployment
216
265
217
- <!-- Deployment -->
266
+ ** WIP **
0 commit comments