mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Convert all API handers to use *context.APIContext
This commit is contained in:
		@@ -6,12 +6,35 @@ package context
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"gopkg.in/macaron.v1"
 | 
			
		||||
 | 
			
		||||
	"github.com/gogits/gogs/modules/base"
 | 
			
		||||
	"github.com/gogits/gogs/modules/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type APIContext struct {
 | 
			
		||||
	*Context
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func APIContexter() macaron.Handler {
 | 
			
		||||
	return func(c *Context) {
 | 
			
		||||
		ctx := &APIContext{
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user