aboutsummaryrefslogtreecommitdiff
path: root/pkg/context/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/context/api.go')
-rw-r--r--pkg/context/api.go24
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)