diff options
author | Unknwon <u@gogs.io> | 2018-12-01 21:41:30 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2018-12-01 21:41:30 -0500 |
commit | 69c1cd3f381b19b988a6af51a8e38303f216b001 (patch) | |
tree | 5b9ce7c21380d8f2a2338516bba55cc75dbac60f /pkg/context/api.go | |
parent | ce13fbb98a68a39b9b08f07bf7c7444e1e869451 (diff) |
routes/api: change status handle to new style
Also fixed one bug that did not catch team not found error.
Diffstat (limited to 'pkg/context/api.go')
-rw-r--r-- | pkg/context/api.go | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/pkg/context/api.go b/pkg/context/api.go index fab66024..7df5b23a 100644 --- a/pkg/context/api.go +++ b/pkg/context/api.go @@ -6,6 +6,7 @@ package context import ( "fmt" + "net/http" "strings" "github.com/Unknwon/paginater" @@ -33,7 +34,7 @@ func (c *APIContext) Error(status int, title string, obj interface{}) { message = obj.(string) } - if status == 500 { + if status == http.StatusInternalServerError { log.Error(3, "%s: %s", title, message) } @@ -43,6 +44,27 @@ func (c *APIContext) Error(status int, title string, obj interface{}) { }) } +// NotFound renders the 404 response. +func (c *APIContext) NotFound() { + c.Status(http.StatusNotFound) +} + +// ServerError renders the 500 response. +func (c *APIContext) ServerError(title string, err error) { + c.Error(http.StatusInternalServerError, title, err) +} + +// NotFoundOrServerError use error check function to determine if the error +// is about not found. It responses with 404 status code for not found error, +// or error context description for logging purpose of 500 server error. +func (c *APIContext) NotFoundOrServerError(title string, errck func(error) bool, err error) { + if errck(err) { + c.NotFound() + return + } + c.ServerError(title, err) +} + // SetLinkHeader sets pagination link header by given total number and page size. func (c *APIContext) SetLinkHeader(total, pageSize int) { page := paginater.New(total, pageSize, c.QueryInt("page"), 0) |