api/handler/rpc: log errors (#1390)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-03-23 10:31:35 +03:00
parent cc8dad8338
commit 244c9fdb90

18
rpc.go
View File

@ -19,6 +19,7 @@ import (
"github.com/micro/go-micro/v2/codec/jsonrpc" "github.com/micro/go-micro/v2/codec/jsonrpc"
"github.com/micro/go-micro/v2/codec/protorpc" "github.com/micro/go-micro/v2/codec/protorpc"
"github.com/micro/go-micro/v2/errors" "github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/util/ctx" "github.com/micro/go-micro/v2/util/ctx"
) )
@ -90,7 +91,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// only allow post when we have the router // only allow post when we have the router
if r.Method != "GET" && (h.opts.Router != nil && r.Method != "POST") { if r.Method != "GET" && (h.opts.Router != nil && r.Method != "POST") {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) writeError(w, r, errors.MethodNotAllowed("go.micro.api", "method not allowed"))
return return
} }
@ -296,7 +297,12 @@ func writeError(w http.ResponseWriter, r *http.Request, err error) {
w.Header().Set("grpc-message", ce.Detail) w.Header().Set("grpc-message", ce.Detail)
} }
w.Write([]byte(ce.Error())) _, werr := w.Write([]byte(ce.Error()))
if err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(werr)
}
}
} }
func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) { func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
@ -312,7 +318,13 @@ func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
} }
// write response // write response
w.Write(rsp) _, err := w.Write(rsp)
if err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(err)
}
}
} }
func NewHandler(opts ...handler.Option) handler.Handler { func NewHandler(opts ...handler.Option) handler.Handler {