api.go 915 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "gopkg.in/macaron.v1"
  7. "github.com/gogits/gogs/modules/base"
  8. "github.com/gogits/gogs/modules/log"
  9. )
  10. type APIContext struct {
  11. *Context
  12. }
  13. // Error responses error message to client with given message.
  14. // If status is 500, also it prints error to log.
  15. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  16. var message string
  17. if err, ok := obj.(error); ok {
  18. message = err.Error()
  19. } else {
  20. message = obj.(string)
  21. }
  22. if status == 500 {
  23. log.Error(4, "%s: %s", title, message)
  24. }
  25. ctx.JSON(status, map[string]string{
  26. "message": message,
  27. "url": base.DOC_URL,
  28. })
  29. }
  30. func APIContexter() macaron.Handler {
  31. return func(c *Context) {
  32. ctx := &APIContext{
  33. Context: c,
  34. }
  35. c.Map(ctx)
  36. }
  37. }