2016-03-13 22:37:44 +01:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/macaron.v1"
|
2016-03-13 23:49:16 +01:00
|
|
|
|
|
|
|
"github.com/gogits/gogs/modules/base"
|
|
|
|
"github.com/gogits/gogs/modules/log"
|
2016-03-13 22:37:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type APIContext struct {
|
|
|
|
*Context
|
|
|
|
}
|
|
|
|
|
2016-03-13 23:49:16 +01:00
|
|
|
// Error responses error message to client with given message.
|
|
|
|
// If status is 500, also it prints error to log.
|
|
|
|
func (ctx *APIContext) Error(status int, title string, obj interface{}) {
|
|
|
|
var message string
|
|
|
|
if err, ok := obj.(error); ok {
|
|
|
|
message = err.Error()
|
|
|
|
} else {
|
|
|
|
message = obj.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == 500 {
|
|
|
|
log.Error(4, "%s: %s", title, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(status, map[string]string{
|
|
|
|
"message": message,
|
|
|
|
"url": base.DOC_URL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-13 22:37:44 +01:00
|
|
|
func APIContexter() macaron.Handler {
|
|
|
|
return func(c *Context) {
|
|
|
|
ctx := &APIContext{
|
|
|
|
Context: c,
|
|
|
|
}
|
|
|
|
c.Map(ctx)
|
|
|
|
}
|
|
|
|
}
|