Skip to content

Commit 360083a

Browse files
committed
add API specific InternalServerError()
1 parent 788b8b1 commit 360083a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

modules/context/api.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package context
77

88
import (
99
"fmt"
10+
"net/http"
1011
"net/url"
1112
"strings"
1213

@@ -64,7 +65,7 @@ type APINotFound struct{}
6465
// swagger:response redirect
6566
type APIRedirect struct{}
6667

67-
// Error responses error message to client with given message.
68+
// Error responds with an error message to client with given obj as the message.
6869
// If status is 500, also it prints error to log.
6970
func (ctx *APIContext) Error(status int, title string, obj interface{}) {
7071
var message string
@@ -74,8 +75,8 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
7475
message = obj.(string)
7576
}
7677

77-
if status == 500 {
78-
log.Error("%s: %s", title, message)
78+
if status == http.StatusInternalServerError {
79+
log.ErrorWithSkip(1, "%s: %s", title, message)
7980
}
8081

8182
ctx.JSON(status, APIError{
@@ -84,6 +85,17 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
8485
})
8586
}
8687

88+
// InternalServerError responds with an error message to the client with the error as a message
89+
// and the file and line of the caller.
90+
func (ctx *APIContext) InternalServerError(err error) {
91+
log.ErrorWithSkip(1, "InternalServerError: %v", err)
92+
93+
ctx.JSON(http.StatusInternalServerError, APIError{
94+
Message: err.Error(),
95+
URL: setting.API.SwaggerURL,
96+
})
97+
}
98+
8799
func genAPILinks(curURL *url.URL, total, pageSize, curPage int) []string {
88100
page := NewPagination(total, pageSize, curPage, 0)
89101
paginater := page.Paginater

0 commit comments

Comments
 (0)