mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 13:19:37 -05:00
add API specific InternalServerError()
This commit is contained in:
parent
788b8b1440
commit
360083a486
1 changed files with 15 additions and 3 deletions
|
@ -7,6 +7,7 @@ package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ type APINotFound struct{}
|
||||||
// swagger:response redirect
|
// swagger:response redirect
|
||||||
type APIRedirect struct{}
|
type APIRedirect struct{}
|
||||||
|
|
||||||
// Error responses error message to client with given message.
|
// Error responds with an error message to client with given obj as the message.
|
||||||
// If status is 500, also it prints error to log.
|
// If status is 500, also it prints error to log.
|
||||||
func (ctx *APIContext) Error(status int, title string, obj interface{}) {
|
func (ctx *APIContext) Error(status int, title string, obj interface{}) {
|
||||||
var message string
|
var message string
|
||||||
|
@ -74,8 +75,8 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
|
||||||
message = obj.(string)
|
message = obj.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
if status == 500 {
|
if status == http.StatusInternalServerError {
|
||||||
log.Error("%s: %s", title, message)
|
log.ErrorWithSkip(1, "%s: %s", title, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(status, APIError{
|
ctx.JSON(status, APIError{
|
||||||
|
@ -84,6 +85,17 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InternalServerError responds with an error message to the client with the error as a message
|
||||||
|
// and the file and line of the caller.
|
||||||
|
func (ctx *APIContext) InternalServerError(err error) {
|
||||||
|
log.ErrorWithSkip(1, "InternalServerError: %v", err)
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusInternalServerError, APIError{
|
||||||
|
Message: err.Error(),
|
||||||
|
URL: setting.API.SwaggerURL,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func genAPILinks(curURL *url.URL, total, pageSize, curPage int) []string {
|
func genAPILinks(curURL *url.URL, total, pageSize, curPage int) []string {
|
||||||
page := NewPagination(total, pageSize, curPage, 0)
|
page := NewPagination(total, pageSize, curPage, 0)
|
||||||
paginater := page.Paginater
|
paginater := page.Paginater
|
||||||
|
|
Loading…
Reference in a new issue