Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
Uniform code style: Context.Error and Context.Redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed May 12, 2020
1 parent 30aefcd commit e5f4a92
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@ Change Log
under development
-----------------
- `Context.Error`, `Context.NotFound`, `Context.Redirect`, `Context.ServeFile`, `Context.ServeContent` return a nil error.
- `Context.Error(msg string, code int)` was changed as `Context.Error(code int, msg string)`.
- `Context.Redirect(url string, code int)` was changed as `Context.Redirect(code int, url string)`.

v1.12.1 May 5, 2020
-------------------
Expand Down
4 changes: 2 additions & 2 deletions context.go
Expand Up @@ -88,7 +88,7 @@ func (ctx *Context) reset() {
}

// Error is a shortcut of http.Error.
func (ctx *Context) Error(msg string, code int) error {
func (ctx *Context) Error(code int, msg string) error {
http.Error(ctx.Response, msg, code)
return nil
}
Expand All @@ -100,7 +100,7 @@ func (ctx *Context) NotFound() error {
}

// Redirect is a shortcut of http.Redirect.
func (ctx *Context) Redirect(url string, code int) error {
func (ctx *Context) Redirect(code int, url string) error {
http.Redirect(ctx.Response, ctx.Request, url, code)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions context_test.go
Expand Up @@ -92,7 +92,7 @@ func TestContext_NotFound(t *testing.T) {
func TestContext_Redirect(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, httptest.NewRequest(http.MethodGet, "/", nil))
assert.Nil(t, ctx.Redirect("/redirect", http.StatusPermanentRedirect))
assert.Nil(t, ctx.Redirect(http.StatusPermanentRedirect, "/redirect"))
assert.Equal(t, http.StatusPermanentRedirect, w.Code)
}
func TestContext_Error(t *testing.T) {
Expand All @@ -107,7 +107,7 @@ func TestContext_Error(t *testing.T) {
for _, test := range tests {
w := httptest.NewRecorder()
ctx := newContext(w, nil)
assert.Nil(t, ctx.Error(test.msg, test.code))
assert.Nil(t, ctx.Error(test.code, test.msg))
assert.Equal(t, fmt.Sprintln(test.msg), w.Body.String())
assert.Equal(t, test.code, w.Code)
}
Expand Down
8 changes: 4 additions & 4 deletions router.go
Expand Up @@ -400,7 +400,7 @@ func (r *Router) handleRequest(ctx *Context) (err error) {
} else {
path = path + "/"
}
ctx.Redirect(path, code)
ctx.Redirect(code, path)
return
}

Expand All @@ -411,7 +411,7 @@ func (r *Router) handleRequest(ctx *Context) (err error) {
r.RedirectTrailingSlash,
)
if found {
ctx.Redirect(fixedPath, code)
ctx.Redirect(code, fixedPath)
return
}
}
Expand Down Expand Up @@ -458,8 +458,8 @@ func (r *Router) HandleError(ctx *Context, err error) {

switch e := err.(type) {
case StatusError:
ctx.Error(err.Error(), e.Status())
ctx.Error(e.Status(), err.Error())
default:
ctx.Error(err.Error(), http.StatusInternalServerError)
ctx.Error(http.StatusInternalServerError, err.Error())
}
}
2 changes: 1 addition & 1 deletion router_test.go
Expand Up @@ -662,7 +662,7 @@ type testErrorHandler struct {
}

func (eh testErrorHandler) Handle(ctx *Context, err error) {
ctx.Error(err.Error(), eh.status)
ctx.Error(eh.status, err.Error())
}

func TestRouter_ErrorHandler(t *testing.T) {
Expand Down

0 comments on commit e5f4a92

Please sign in to comment.