responses.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package httpx
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "sync"
  6. "github.com/tal-tech/go-zero/core/logx"
  7. )
  8. var (
  9. errorHandler func(error) (int, interface{})
  10. lock sync.RWMutex
  11. )
  12. func Error(w http.ResponseWriter, err error) {
  13. lock.RLock()
  14. handler := errorHandler
  15. lock.RUnlock()
  16. if handler == nil {
  17. http.Error(w, err.Error(), http.StatusBadRequest)
  18. return
  19. }
  20. code, body := errorHandler(err)
  21. e, ok := body.(error)
  22. if ok {
  23. http.Error(w, e.Error(), code)
  24. } else {
  25. WriteJson(w, code, body)
  26. }
  27. }
  28. func Ok(w http.ResponseWriter) {
  29. w.WriteHeader(http.StatusOK)
  30. }
  31. func OkJson(w http.ResponseWriter, v interface{}) {
  32. WriteJson(w, http.StatusOK, v)
  33. }
  34. func SetErrorHandler(handler func(error) (int, interface{})) {
  35. lock.Lock()
  36. defer lock.Unlock()
  37. errorHandler = handler
  38. }
  39. func WriteJson(w http.ResponseWriter, code int, v interface{}) {
  40. w.Header().Set(ContentType, ApplicationJson)
  41. w.WriteHeader(code)
  42. if bs, err := json.Marshal(v); err != nil {
  43. http.Error(w, err.Error(), http.StatusInternalServerError)
  44. } else if n, err := w.Write(bs); err != nil {
  45. // http.ErrHandlerTimeout has been handled by http.TimeoutHandler,
  46. // so it's ignored here.
  47. if err != http.ErrHandlerTimeout {
  48. logx.Errorf("write response failed, error: %s", err)
  49. }
  50. } else if n < len(bs) {
  51. logx.Errorf("actual bytes: %d, written bytes: %d", len(bs), n)
  52. }
  53. }