Skip to content

Commit 92e7a6c

Browse files
committed
Added errors
Signed-off-by: Vishal Rana <[email protected]>
1 parent 9d64eb9 commit 92e7a6c

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Echo is a fast HTTP router (zero memory allocation) + micro web framework in Go.
1616
- `http.Handler`
1717
- `http.HandlerFunc`
1818
- `func(http.ResponseWriter, *http.Request)`
19-
- Sub/Group router
19+
- Sub/Group routing
2020
- Handy encoding/decoding functions.
2121
- Serve static files, including index.
2222

@@ -56,10 +56,14 @@ func init() {
5656

5757
func createUser(c *echo.Context) {
5858
u := new(user)
59-
if c.Bind(u) {
59+
if err := c.Bind(u); err == nil {
6060
users[u.ID] = *u
61-
c.JSON(http.StatusCreated, u)
61+
if err := c.JSON(http.StatusCreated, u); err == nil {
62+
// Do something!
63+
}
64+
return
6265
}
66+
http.Error(c.Response, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
6367
}
6468

6569
func getUsers(c *echo.Context) {

context.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,17 @@ func (c *Context) Param(n string) string {
3131
}
3232

3333
// Bind decodes the payload into provided type based on Content-Type header.
34-
func (c *Context) Bind(i interface{}) bool {
35-
var err error
34+
func (c *Context) Bind(i interface{}) (err error) {
3635
ct := c.Request.Header.Get(HeaderContentType)
3736
if strings.HasPrefix(ct, MIMEJSON) {
3837
dec := json.NewDecoder(c.Request.Body)
39-
err = dec.Decode(i)
38+
if err = dec.Decode(i); err != nil {
39+
err = ErrBindJSON
40+
}
4041
} else {
41-
// TODO:
42+
err = ErrUnsupportedContentType
4243
}
43-
if err != nil {
44-
c.echo.internalServerErrorHandler(c)
45-
return false
46-
}
47-
return true
44+
return
4845
}
4946

5047
// String sends a text/plain response with status code.
@@ -55,13 +52,14 @@ func (c *Context) String(n int, s string) {
5552
}
5653

5754
// JSON sends an application/json response with status code.
58-
func (c *Context) JSON(n int, i interface{}) {
55+
func (c *Context) JSON(n int, i interface{}) (err error) {
5956
enc := json.NewEncoder(c.Response)
6057
c.Response.Header().Set(HeaderContentType, MIMEJSON+"; charset=utf-8")
6158
c.Response.WriteHeader(n)
6259
if err := enc.Encode(i); err != nil {
63-
c.echo.internalServerErrorHandler(c)
60+
err = ErrRenderJSON
6461
}
62+
return
6563
}
6664

6765
// func (c *Context) File(n int, file, name string) {

echo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package echo
22

33
import (
4+
"errors"
45
"log"
56
"net/http"
67
"sync"
@@ -55,6 +56,11 @@ var (
5556
MethodPUT,
5657
MethodTRACE,
5758
}
59+
60+
// Errors
61+
ErrUnsupportedContentType = errors.New("echo: unsupported content type")
62+
ErrBindJSON = errors.New("echo: bind json error")
63+
ErrRenderJSON = errors.New("echo: render json error")
5864
)
5965

6066
// New creates a echo instance.

example/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ func init() {
2727

2828
func createUser(c *echo.Context) {
2929
u := new(user)
30-
if c.Bind(u) {
30+
if err := c.Bind(u); err == nil {
3131
users[u.ID] = *u
32-
c.JSON(http.StatusCreated, u)
32+
if err := c.JSON(http.StatusCreated, u); err == nil {
33+
// Do something!
34+
}
35+
return
3336
}
37+
http.Error(c.Response, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
3438
}
3539

3640
func getUsers(c *echo.Context) {

0 commit comments

Comments
 (0)