content_ext.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package service
  2. import (
  3. "go-common/library/ecode"
  4. bm "go-common/library/net/http/blademaster"
  5. "go-common/library/net/http/blademaster/render"
  6. "net/http"
  7. )
  8. //BmHTTPErrorWithMsg return customed message to client
  9. func BmHTTPErrorWithMsg(c *bm.Context, err error, msg string) {
  10. if c.IsAborted() {
  11. return
  12. }
  13. c.Error = err
  14. bcode := ecode.Cause(err)
  15. if msg == "" {
  16. msg = err.Error()
  17. }
  18. c.Render(http.StatusOK, render.JSON{
  19. Code: bcode.Code(),
  20. Message: msg,
  21. Data: nil,
  22. })
  23. }
  24. //BmGetStringOrDefault util to get string from context
  25. func BmGetStringOrDefault(c *bm.Context, key string, defaul string) (value string, exist bool) {
  26. i, exist := c.Get(key)
  27. if !exist {
  28. value = defaul
  29. return
  30. }
  31. value, exist = i.(string)
  32. if !exist {
  33. value = defaul
  34. }
  35. return
  36. }
  37. //BmGetInt64OrDefault util to get int64 from context
  38. func BmGetInt64OrDefault(c *bm.Context, key string, defaul int64) (value int64, exist bool) {
  39. i, exist := c.Get(key)
  40. if !exist {
  41. value = defaul
  42. return
  43. }
  44. value, exist = i.(int64)
  45. if !exist {
  46. value = defaul
  47. }
  48. return
  49. }