Skip to content

Commit 74158e5

Browse files
authored
fix: remove error handler (#42)
1 parent 47bcf65 commit 74158e5

13 files changed

+85
-224
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func main() {
3030
k.Run()
3131
}
3232

33-
func helloHandler(c *kid.Context) error {
34-
return c.JSON(http.StatusOK, kid.Map{"message": "Hello Kid!"})
33+
func helloHandler(c *kid.Context) {
34+
c.JSON(http.StatusOK, kid.Map{"message": "Hello Kid!"})
3535
}
3636
```
3737

Diff for: defaults.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,20 @@ package kid
22

33
import (
44
"net/http"
5-
6-
"github.com/mojixcoder/kid/errors"
75
)
86

97
var (
108
// defaultNotFoundHandler is Kid's default not found handler.
119
//
1210
// It will be used when request doesn't match any routes.
13-
defaultNotFoundHandler HandlerFunc = func(c *Context) error {
14-
err := errors.NewHTTPError(http.StatusNotFound)
15-
return err
11+
defaultNotFoundHandler HandlerFunc = func(c *Context) {
12+
c.JSON(http.StatusNotFound, Map{"message": http.StatusText(http.StatusNotFound)})
1613
}
1714

1815
// defaultMethodNotAllowedHandler is Kid's default method not allowed handler.
1916
//
2017
// It will be used when request matches a route but its method doesn't match route's method.
21-
defaultMethodNotAllowedHandler HandlerFunc = func(c *Context) error {
22-
err := errors.NewHTTPError(http.StatusMethodNotAllowed)
23-
return err
24-
}
25-
26-
// defaultErrorHandler is Kid's default error handler.
27-
//
28-
// It will be used when handlers return an error.
29-
// It can send proper responses when an HTTP error is returned.
30-
defaultErrorHandler ErrorHandler = func(c *Context, err error) {
31-
httpErr, ok := err.(*errors.HTTPError)
32-
if !ok {
33-
httpErr = errors.NewHTTPError(http.StatusInternalServerError)
34-
}
35-
36-
if c.Request().Method == http.MethodHead {
37-
c.NoContent(httpErr.Code)
38-
} else {
39-
c.JSON(httpErr.Code, httpErr)
40-
}
18+
defaultMethodNotAllowedHandler HandlerFunc = func(c *Context) {
19+
c.JSON(http.StatusMethodNotAllowed, Map{"message": http.StatusText(http.StatusMethodNotAllowed)})
4120
}
4221
)

Diff for: defaults_test.go

+3-54
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
11
package kid
22

33
import (
4-
"errors"
54
"net/http"
65
"net/http/httptest"
76
"testing"
87

9-
kiderrors "github.com/mojixcoder/kid/errors"
108
"github.com/stretchr/testify/assert"
119
)
1210

1311
func setupKid() *Kid {
1412
k := New()
1513

16-
k.Post("/post", func(c *Context) error {
17-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
18-
})
19-
20-
k.Get("/http-error", func(c *Context) error {
21-
return kiderrors.NewHTTPError(http.StatusBadRequest)
22-
})
23-
24-
k.Get("/error", func(c *Context) error {
25-
return errors.New("something went wrong")
26-
})
27-
28-
k.Head("/error-head", func(c *Context) error {
29-
return kiderrors.NewHTTPError(http.StatusBadRequest)
14+
k.Post("/post", func(c *Context) {
15+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
3016
})
3117

3218
return k
@@ -36,7 +22,7 @@ func TestDefaultNotFoundHandler(t *testing.T) {
3622
k := setupKid()
3723

3824
w := httptest.NewRecorder()
39-
req, err := http.NewRequest(http.MethodGet, "/not_found", nil)
25+
req, err := http.NewRequest(http.MethodGet, "/not-found", nil)
4026
assert.NoError(t, err)
4127

4228
k.ServeHTTP(w, req)
@@ -59,40 +45,3 @@ func TestDefaultMethodNotAllowedHandler(t *testing.T) {
5945
assert.Equal(t, "{\"message\":\"Method Not Allowed\"}\n", w.Body.String())
6046
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
6147
}
62-
63-
func TestDefaultErrorHandler(t *testing.T) {
64-
k := setupKid()
65-
66-
// Retuns a proper response based on return HTTP error.
67-
w := httptest.NewRecorder()
68-
req, err := http.NewRequest(http.MethodGet, "/http-error", nil)
69-
assert.NoError(t, err)
70-
71-
k.ServeHTTP(w, req)
72-
73-
assert.Equal(t, http.StatusBadRequest, w.Code)
74-
assert.Equal(t, "{\"message\":\"Bad Request\"}\n", w.Body.String())
75-
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
76-
77-
// Always internal server error is returned when returned error is not a HTTP error.
78-
w = httptest.NewRecorder()
79-
req, err = http.NewRequest(http.MethodGet, "/error", nil)
80-
assert.NoError(t, err)
81-
82-
k.ServeHTTP(w, req)
83-
84-
assert.Equal(t, http.StatusInternalServerError, w.Code)
85-
assert.Equal(t, "{\"message\":\"Internal Server Error\"}\n", w.Body.String())
86-
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
87-
88-
// No body is returned when request method is HEAD.
89-
w = httptest.NewRecorder()
90-
req, err = http.NewRequest(http.MethodHead, "/error-head", nil)
91-
assert.NoError(t, err)
92-
93-
k.ServeHTTP(w, req)
94-
95-
assert.Equal(t, http.StatusBadRequest, w.Code)
96-
assert.Equal(t, "", w.Body.String())
97-
assert.Equal(t, "", w.Header().Get("Content-Type"))
98-
}

Diff for: examples/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ func main() {
1414
k.Run()
1515
}
1616

17-
func helloHandler(c *kid.Context) error {
18-
return c.JSON(http.StatusOK, kid.Map{"message": "Hello Kid!"})
17+
func helloHandler(c *kid.Context) {
18+
c.JSON(http.StatusOK, kid.Map{"message": "Hello Kid!"})
1919
}

Diff for: group_test.go

+26-26
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,44 @@ import (
1010
)
1111

1212
func registerHandlers(g Group) {
13-
g.Get("/path", func(c *Context) error {
14-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
13+
g.Get("/path", func(c *Context) {
14+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
1515
})
1616

17-
g.Post("/path", func(c *Context) error {
18-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
17+
g.Post("/path", func(c *Context) {
18+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
1919
})
2020

21-
g.Patch("/path", func(c *Context) error {
22-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
21+
g.Patch("/path", func(c *Context) {
22+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
2323
})
2424

25-
g.Put("/path", func(c *Context) error {
26-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
25+
g.Put("/path", func(c *Context) {
26+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
2727
})
2828

29-
g.Delete("/path", func(c *Context) error {
30-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
29+
g.Delete("/path", func(c *Context) {
30+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
3131
})
3232

33-
g.Connect("/path", func(c *Context) error {
34-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
33+
g.Connect("/path", func(c *Context) {
34+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
3535
})
3636

37-
g.Trace("/path", func(c *Context) error {
38-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
37+
g.Trace("/path", func(c *Context) {
38+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
3939
})
4040

41-
g.Options("/path", func(c *Context) error {
42-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
41+
g.Options("/path", func(c *Context) {
42+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
4343
})
4444

45-
g.Head("/path", func(c *Context) error {
46-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
45+
g.Head("/path", func(c *Context) {
46+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
4747
})
4848

49-
g.Any("/any", func(c *Context) error {
50-
return c.JSON(http.StatusOK, Map{"method": c.Request().Method})
49+
g.Any("/any", func(c *Context) {
50+
c.JSON(http.StatusOK, Map{"method": c.Request().Method})
5151
})
5252
}
5353

@@ -96,8 +96,8 @@ func TestGroup_Add(t *testing.T) {
9696
g.Add("/", nil, []string{http.MethodGet, http.MethodPost})
9797
})
9898

99-
g.Add("/test", func(c *Context) error {
100-
return c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
99+
g.Add("/test", func(c *Context) {
100+
c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
101101
}, []string{http.MethodGet, http.MethodPost})
102102

103103
assert.Equal(t, 1, len(k.router.routes))
@@ -184,12 +184,12 @@ func TestGroup_Add_NestedGroups(t *testing.T) {
184184
g := newGroup(k, "/v1")
185185
nestedG := g.Group("/api")
186186

187-
g.Add("/test", func(c *Context) error {
188-
return c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
187+
g.Add("/test", func(c *Context) {
188+
c.JSON(http.StatusCreated, Map{"message": c.Request().Method})
189189
}, []string{http.MethodPost})
190190

191-
nestedG.Add("/{var}", func(c *Context) error {
192-
return c.JSON(http.StatusCreated, Map{"message": c.Param("var")})
191+
nestedG.Add("/{var}", func(c *Context) {
192+
c.JSON(http.StatusCreated, Map{"message": c.Param("var")})
193193
}, []string{http.MethodPost})
194194

195195
testCases := []struct {

Diff for: kid.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ import (
1616

1717
type (
1818
// HandlerFunc is the type which serves HTTP requests.
19-
HandlerFunc func(c *Context) error
19+
HandlerFunc func(c *Context)
2020

2121
// MiddlewareFunc is the type of middlewares.
2222
MiddlewareFunc func(next HandlerFunc) HandlerFunc
2323

24-
// ErrorHandler is the functions that handles errors when a handler returns an error.
25-
ErrorHandler func(c *Context, err error)
26-
2724
// Map is a generic map to make it easier to send responses.
2825
Map map[string]any
2926

@@ -35,7 +32,6 @@ type (
3532
middlewares []MiddlewareFunc
3633
notFoundHandler HandlerFunc
3734
methodNotAllowedHandler HandlerFunc
38-
errorHandler ErrorHandler
3935
jsonSerializer serializer.Serializer
4036
xmlSerializer serializer.Serializer
4137
htmlRenderer htmlrenderer.HTMLRenderer
@@ -61,7 +57,6 @@ func New() *Kid {
6157
middlewares: make([]MiddlewareFunc, 0),
6258
notFoundHandler: defaultNotFoundHandler,
6359
methodNotAllowedHandler: defaultMethodNotAllowedHandler,
64-
errorHandler: defaultErrorHandler,
6560
jsonSerializer: serializer.NewJSONSerializer(),
6661
xmlSerializer: serializer.NewXMLSerializer(),
6762
htmlRenderer: htmlrenderer.Default(false),
@@ -219,9 +214,7 @@ func (k *Kid) ServeHTTP(w http.ResponseWriter, r *http.Request) {
219214
handler = k.applyMiddlewaresToHandler(handler, k.middlewares...)
220215
}
221216

222-
if err := handler(c); err != nil {
223-
k.errorHandler(c, err)
224-
}
217+
handler(c)
225218

226219
if !c.Response().Written() {
227220
c.Response().WriteHeaderNow()

0 commit comments

Comments
 (0)