go-zero/rest/httpx/responses.go

70 lines
1.6 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package httpx
import (
"encoding/json"
"net/http"
2020-11-17 17:11:06 +08:00
"sync"
2020-07-26 17:09:05 +08:00
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/logx"
2020-07-26 17:09:05 +08:00
)
2020-11-17 17:11:06 +08:00
var (
2020-11-17 18:04:48 +08:00
errorHandler func(error) (int, interface{})
2020-11-17 17:11:06 +08:00
lock sync.RWMutex
)
2021-03-01 19:15:35 +08:00
// Error writes err into w.
2020-07-31 17:03:19 +08:00
func Error(w http.ResponseWriter, err error) {
2020-11-17 17:11:06 +08:00
lock.RLock()
2020-11-17 18:04:48 +08:00
handler := errorHandler
2020-11-17 17:11:06 +08:00
lock.RUnlock()
2020-11-17 18:04:48 +08:00
if handler == nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
code, body := errorHandler(err)
e, ok := body.(error)
if ok {
http.Error(w, e.Error(), code)
} else {
WriteJson(w, code, body)
}
2020-07-31 17:03:19 +08:00
}
2021-03-01 19:15:35 +08:00
// Ok writes HTTP 200 OK into w.
2020-07-31 17:03:19 +08:00
func Ok(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
}
2021-03-01 19:15:35 +08:00
// OkJson writes v into w with 200 OK.
2020-07-26 17:09:05 +08:00
func OkJson(w http.ResponseWriter, v interface{}) {
WriteJson(w, http.StatusOK, v)
}
2021-03-01 19:15:35 +08:00
// SetErrorHandler sets the error handler, which is called on calling Error.
2020-11-17 17:11:06 +08:00
func SetErrorHandler(handler func(error) (int, interface{})) {
lock.Lock()
defer lock.Unlock()
errorHandler = handler
}
2021-03-01 19:15:35 +08:00
// WriteJson writes v as json string into w with code.
2020-07-26 17:09:05 +08:00
func WriteJson(w http.ResponseWriter, code int, v interface{}) {
w.Header().Set(ContentType, ApplicationJson)
w.WriteHeader(code)
if bs, err := json.Marshal(v); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else if n, err := w.Write(bs); err != nil {
// http.ErrHandlerTimeout has been handled by http.TimeoutHandler,
// so it's ignored here.
if err != http.ErrHandlerTimeout {
logx.Errorf("write response failed, error: %s", err)
}
} else if n < len(bs) {
logx.Errorf("actual bytes: %d, written bytes: %d", len(bs), n)
}
}