Skip to content

Commit a4375c4

Browse files
committed
Fixed examples with new HandlerFunc changes
Signed-off-by: Vishal Rana <[email protected]>
1 parent 13ac746 commit a4375c4

File tree

8 files changed

+66
-64
lines changed

8 files changed

+66
-64
lines changed

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
99
- `echo.MiddlewareFunc`
1010
- `func(echo.HandlerFunc) echo.HandlerFunc`
1111
- `echo.HandlerFunc`
12-
- `func(*echo.Context) *echo.HTTPError`
12+
- `func(*echo.Context) error`
1313
- `func(http.Handler) http.Handler`
1414
- `http.Handler`
1515
- `http.HandlerFunc`
1616
- `func(http.ResponseWriter, *http.Request)`
1717
- Handler
1818
- `echo.HandlerFunc`
19-
- `func(*echo.Context) *echo.HTTPError`
19+
- `func(*echo.Context) error`
2020
- `http.Handler`
2121
- `http.HandlerFunc`
2222
- `func(http.ResponseWriter, *http.Request)`
@@ -84,7 +84,7 @@ import (
8484
)
8585

8686
// Handler
87-
func hello(c *echo.Context) *echo.HTTPError {
87+
func hello(c *echo.Context) error {
8888
return c.String(http.StatusOK, "Hello, World!\n")
8989
}
9090

@@ -218,22 +218,19 @@ var (
218218
)
219219

220220
// Render HTML
221-
func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTPError {
222-
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
223-
return &echo.HTTPError{Error: err}
224-
}
225-
return nil
221+
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
222+
return t.templates.ExecuteTemplate(w, name, data)
226223
}
227224

228225
//----------
229226
// Handlers
230227
//----------
231228

232-
func welcome(c *echo.Context) *echo.HTTPError {
229+
func welcome(c *echo.Context) error {
233230
return c.Render(http.StatusOK, "welcome", "Joe")
234231
}
235232

236-
func createUser(c *echo.Context) *echo.HTTPError {
233+
func createUser(c *echo.Context) error {
237234
u := new(user)
238235
if err := c.Bind(u); err != nil {
239236
return err
@@ -242,11 +239,11 @@ func createUser(c *echo.Context) *echo.HTTPError {
242239
return c.JSON(http.StatusCreated, u)
243240
}
244241

245-
func getUsers(c *echo.Context) *echo.HTTPError {
242+
func getUsers(c *echo.Context) error {
246243
return c.JSON(http.StatusOK, users)
247244
}
248245

249-
func getUser(c *echo.Context) *echo.HTTPError {
246+
func getUser(c *echo.Context) error {
250247
return c.JSON(http.StatusOK, users[c.P(0)])
251248
}
252249

@@ -268,7 +265,7 @@ func main() {
268265
s := stats.New()
269266
e.Use(s.Handler)
270267
// Route
271-
e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
268+
e.Get("/stats", func(c *echo.Context) error {
272269
return c.JSON(http.StatusOK, s.Data())
273270
})
274271

@@ -297,7 +294,7 @@ func main() {
297294
// Cached templates
298295
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
299296
}
300-
e.Renderer(t)
297+
e.SetRenderer(t)
301298
e.Get("/welcome", welcome)
302299

303300
//-------
@@ -306,20 +303,20 @@ func main() {
306303

307304
// Group with parent middleware
308305
a := e.Group("/admin")
309-
a.Use(func(c *echo.Context) *echo.HTTPError {
306+
a.Use(func(c *echo.Context) error {
310307
// Security middleware
311308
return nil
312309
})
313-
a.Get("", func(c *echo.Context) *echo.HTTPError {
310+
a.Get("", func(c *echo.Context) error {
314311
return c.String(http.StatusOK, "Welcome admin!")
315312
})
316313

317314
// Group with no parent middleware
318-
g := e.Group("/files", func(c *echo.Context) *echo.HTTPError {
315+
g := e.Group("/files", func(c *echo.Context) error {
319316
// Security middleware
320317
return nil
321318
})
322-
g.Get("", func(c *echo.Context) *echo.HTTPError {
319+
g.Get("", func(c *echo.Context) error {
323320
return c.String(http.StatusOK, "Your files!")
324321
})
325322

@@ -350,7 +347,7 @@ import (
350347
)
351348

352349
// Handler
353-
func hello(c *echo.Context) *echo.HTTPError {
350+
func hello(c *echo.Context) error {
354351
return c.String(http.StatusOK, "Hello, World!\n")
355352
}
356353

@@ -359,7 +356,7 @@ func main() {
359356
e := echo.New()
360357

361358
// Debug mode
362-
e.Debug(true)
359+
e.SetDebug(true)
363360

364361
//------------
365362
// Middleware

echo.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ var (
120120
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
121121
)
122122

123+
func NewHTTPError(code int, msgs ...string) *HTTPError {
124+
he := &HTTPError{Code: code}
125+
if len(msgs) == 0 {
126+
he.Message = http.StatusText(code)
127+
}
128+
return he
129+
}
130+
123131
func (e *HTTPError) Error() string {
124132
return e.Message
125133
}
@@ -140,7 +148,7 @@ func New() (e *Echo) {
140148

141149
e.SetMaxParam(5)
142150
e.notFoundHandler = func(c *Context) error {
143-
return &HTTPError{Code: http.StatusNotFound}
151+
return NewHTTPError(http.StatusNotFound)
144152
}
145153
e.SetHTTPErrorHandler(func(err error, c *Context) {
146154
code := http.StatusInternalServerError

examples/hello/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// Handler
11-
func hello(c *echo.Context) *echo.HTTPError {
11+
func hello(c *echo.Context) error {
1212
return c.String(http.StatusOK, "Hello, World!\n")
1313
}
1414

examples/middleware/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// Handler
11-
func hello(c *echo.Context) *echo.HTTPError {
11+
func hello(c *echo.Context) error {
1212
return c.String(http.StatusOK, "Hello, World!\n")
1313
}
1414

examples/website/server.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,19 @@ var (
2929
)
3030

3131
// Render HTML
32-
func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTPError {
33-
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
34-
return &echo.HTTPError{Error: err}
35-
}
36-
return nil
32+
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
33+
return t.templates.ExecuteTemplate(w, name, data)
3734
}
3835

3936
//----------
4037
// Handlers
4138
//----------
4239

43-
func welcome(c *echo.Context) *echo.HTTPError {
40+
func welcome(c *echo.Context) error {
4441
return c.Render(http.StatusOK, "welcome", "Joe")
4542
}
4643

47-
func createUser(c *echo.Context) *echo.HTTPError {
44+
func createUser(c *echo.Context) error {
4845
u := new(user)
4946
if err := c.Bind(u); err != nil {
5047
return err
@@ -53,11 +50,11 @@ func createUser(c *echo.Context) *echo.HTTPError {
5350
return c.JSON(http.StatusCreated, u)
5451
}
5552

56-
func getUsers(c *echo.Context) *echo.HTTPError {
53+
func getUsers(c *echo.Context) error {
5754
return c.JSON(http.StatusOK, users)
5855
}
5956

60-
func getUser(c *echo.Context) *echo.HTTPError {
57+
func getUser(c *echo.Context) error {
6158
return c.JSON(http.StatusOK, users[c.P(0)])
6259
}
6360

@@ -79,7 +76,7 @@ func main() {
7976
s := stats.New()
8077
e.Use(s.Handler)
8178
// Route
82-
e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
79+
e.Get("/stats", func(c *echo.Context) error {
8380
return c.JSON(http.StatusOK, s.Data())
8481
})
8582

@@ -117,20 +114,20 @@ func main() {
117114

118115
// Group with parent middleware
119116
a := e.Group("/admin")
120-
a.Use(func(c *echo.Context) *echo.HTTPError {
117+
a.Use(func(c *echo.Context) error {
121118
// Security middleware
122119
return nil
123120
})
124-
a.Get("", func(c *echo.Context) *echo.HTTPError {
121+
a.Get("", func(c *echo.Context) error {
125122
return c.String(http.StatusOK, "Welcome admin!")
126123
})
127124

128125
// Group with no parent middleware
129-
g := e.Group("/files", func(c *echo.Context) *echo.HTTPError {
126+
g := e.Group("/files", func(c *echo.Context) error {
130127
// Security middleware
131128
return nil
132129
})
133-
g.Get("", func(c *echo.Context) *echo.HTTPError {
130+
g.Get("", func(c *echo.Context) error {
134131
return c.String(http.StatusOK, "Your files!")
135132
})
136133

middleware/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func BasicAuth(fn AuthFunc) echo.HandlerFunc {
1919
return func(c *echo.Context) error {
2020
auth := c.Request.Header.Get(echo.Authorization)
2121
i := 0
22-
he := &echo.HTTPError{Code: http.StatusUnauthorized}
22+
he := echo.NewHTTPError(http.StatusUnauthorized)
2323

2424
for ; i < len(auth); i++ {
2525
c := auth[i]

website/docs/guide.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ for many use cases. Restricting path parameters allows us to use memory efficien
4141

4242
Registers a custom `Echo.HTTPErrorHandler`.
4343

44-
Default handler sends `HTTPError.Message` HTTP response with `HTTPError.Code` status
45-
code.
44+
Default handler rules
4645

47-
- If HTTPError.Code is not set it uses `500`".
48-
- If HTTPError.Message is not set it uses status code text.
49-
- If debug mode is enabled, HTTPError.Message is set to `HTTPError.Error.Error()`.
46+
- If error is of type `Echo.HTTPError` it sends HTTP response with status code `HTTPError.Code`
47+
and message `HTTPError.Message`.
48+
- Else it sends `500 - Internal Server Error`.
49+
- If debug mode is enabled, it uses `error.Error()` as status message.
5050

5151
### Debug
5252

53-
`echo.SetDebug(on bool)`
53+
`Echo.SetDebug(on bool)`
5454

5555
Enables debug mode.
5656

@@ -67,12 +67,12 @@ code below registers a route for method `GET`, path `/hello` and a handler which
6767
`Hello!` HTTP response.
6868

6969
```go
70-
echo.Get("/hello", func(*echo.Context) *HTTPError {
70+
echo.Get("/hello", func(*echo.Context) error {
7171
return c.String(http.StatusOK, "Hello!")
7272
})
7373
```
7474

75-
Echo's default handler is `func(*echo.Context) *echo.HTTPError` where `echo.Context`
75+
Echo's default handler is `func(*echo.Context) error` where `echo.Context`
7676
primarily holds HTTP request and response objects. Echo also has a support for other
7777
types of handlers.
7878

@@ -89,7 +89,7 @@ or by index `echo.Context.P(i uint8) string`. Getting parameter by index gives a
8989
slightly better performance.
9090

9191
```go
92-
echo.Get("/users/:id", func(c *echo.Context) *HTTPError {
92+
echo.Get("/users/:id", func(c *echo.Context) error {
9393
// By name
9494
id := c.Param("id")
9595

@@ -119,15 +119,15 @@ match
119119
#### Example
120120

121121
```go
122-
e.Get("/users/:id", func(c *echo.Context) *HTTPError {
122+
e.Get("/users/:id", func(c *echo.Context) error {
123123
return c.String(http.StatusOK, "/users/:id")
124124
})
125125

126-
e.Get("/users/new", func(c *echo.Context) *HTTPError {
126+
e.Get("/users/new", func(c *echo.Context) error {
127127
return c.String(http.StatusOK, "/users/new")
128128
})
129129

130-
e.Get("/users/1/files/*", func(c *echo.Context) *HTTPError {
130+
e.Get("/users/1/files/*", func(c *echo.Context) error {
131131
return c.String(http.StatusOK, "/users/1/files/*")
132132
})
133133
```
@@ -152,7 +152,7 @@ application.
152152

153153
```go
154154
// Handler
155-
h := func(*echo.Context) *HTTPError {
155+
h := func(*echo.Context) error {
156156
return c.String(http.StatusOK, "OK")
157157
}
158158

@@ -169,23 +169,23 @@ e.Get("/users/:id", h)
169169
### JSON
170170

171171
```go
172-
context.JSON(code int, v interface{}) *HTTPError
172+
context.JSON(code int, v interface{}) error
173173
```
174174

175175
Sends a JSON HTTP response with status code.
176176

177177
### String
178178

179179
```go
180-
context.String(code int, s string) *HTTPError
180+
context.String(code int, s string) error
181181
```
182182

183183
Sends a text/plain HTTP response with status code.
184184

185185
### HTML
186186

187187
```go
188-
func (c *Context) HTML(code int, html string) *HTTPError
188+
func (c *Context) HTML(code int, html string) error
189189
```
190190

191191
Sends an HTML HTTP response with status code.
@@ -228,17 +228,17 @@ e.Favicon("public/favicon.ico")
228228

229229
## Error Handling
230230

231-
Echo advocates centralized HTTP error handling by returning `*echo.HTTPError` from
232-
middleware and handlers.
231+
Echo advocates centralized HTTP error handling by returning `error` from middleware
232+
and handlers.
233233

234234
It allows you to
235235

236236
- Debug by writing stack trace to the HTTP response.
237237
- Customize HTTP responses.
238238
- Recover from panics inside middleware or handlers.
239239

240-
For example, when a basic auth middleware finds invalid credentials it returns 401
241-
"Unauthorized" error, aborting the current HTTP request.
240+
For example, when a basic auth middleware finds invalid credentials it returns
241+
`401 - Unauthorized` error, aborting the current HTTP request.
242242

243243
```go
244244
package main
@@ -251,18 +251,18 @@ import (
251251

252252
func main() {
253253
e := echo.New()
254-
e.Use(func(c *echo.Context) *echo.HTTPError {
254+
e.Use(func(c *echo.Context) error {
255255
// Extract the credentials from HTTP request header and perform a security
256256
// check
257257

258258
// For invalid credentials
259-
return &echo.HTTPError{Code: http.StatusUnauthorized}
259+
return echo.NewHTTPError(http.StatusUnauthorized)
260260
})
261261
e.Get("/welcome", welcome)
262262
e.Run(":1323")
263263
}
264264

265-
func welcome(c *echo.Context) *echo.HTTPError {
265+
func welcome(c *echo.Context) error {
266266
return c.String(http.StatusOK, "Welcome!")
267267
}
268268
```

0 commit comments

Comments
 (0)