-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
79 lines (67 loc) · 2.48 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package runtime
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hopeio/cherry/protobuf/errcode"
httpi "github.com/hopeio/cherry/utils/net/http"
"github.com/hopeio/cherry/utils/net/http/grpc/gateway"
"github.com/hopeio/cherry/utils/net/http/grpc/reconn"
stringsi "github.com/hopeio/cherry/utils/strings"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
"io"
"net/http"
"strings"
)
func RoutingErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, httpStatus int) {
w.WriteHeader(httpStatus)
w.Header().Set("Content-Type", "text/xml; charset=utf-8")
w.Write(stringsi.ToBytes(http.StatusText(httpStatus)))
}
func CustomHttpError(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
s, ok := status.FromError(err)
if ok && s.Code() == 14 && strings.HasSuffix(s.Message(), `refused it."`) {
//提供一个思路,这里应该是哪条连接失败重连哪条,不能这么粗暴,map的key是个关键
if len(reconn.ReConnectMap) > 0 {
for _, f := range reconn.ReConnectMap {
f()
}
}
}
const fallback = `{"code": 14, "message": "failed to marshal error message"}`
w.Header().Del(httpi.HeaderTrailer)
contentType := marshaler.ContentType(nil)
w.Header().Set(httpi.HeaderContentType, contentType)
se, ok := err.(*errcode.ErrRep)
if !ok {
se = &errcode.ErrRep{Code: errcode.Unknown, Message: err.Error()}
}
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
grpclog.Infof("Failed to extract ServerMetadata from context")
}
gateway.HandleForwardResponseServerMetadata(w, md.HeaderMD)
buf, merr := marshaler.Marshal(se)
if merr != nil {
grpclog.Infof("Failed to marshal error message %q: %v", se, merr)
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, fallback); err != nil {
grpclog.Infof("Failed to write response: %v", err)
}
return
}
var wantsTrailers bool
if te := r.Header.Get(httpi.HeaderTE); strings.Contains(strings.ToLower(te), "trailers") {
wantsTrailers = true
gateway.HandleForwardResponseTrailerHeader(w, md.TrailerMD)
w.Header().Set(httpi.HeaderTransferEncoding, "chunked")
}
/* st := HTTPStatusFromCode(se.Code)
w.WriteHeader(st)*/
if _, err := w.Write(buf); err != nil {
grpclog.Infof("Failed to write response: %v", err)
}
if wantsTrailers {
gateway.HandleForwardResponseTrailer(w, md.TrailerMD)
}
}