MSP-5160 refactoring writeResponse
This commit is contained in:
1 parent
57e788e7dc
commit
1999ea366b
1 file changed
+11
-9
+11
-9
@@ -95,13 +95,13 @@ func (p Grpc) Handle(ctx *request.Context) error {
|
||||
Body: &isp.Message_BytesBody{BytesBody: body},
|
||||
})
|
||||
if err != nil {
|
||||
return p.handleError(err, requestId, ctx.ResponseWriter())
|
||||
return p.handleError(err, ctx.ResponseWriter())
|
||||
}
|
||||
|
||||
return p.writeResponse(http.StatusOK, result.GetBytesBody(), requestid.FromContext(ctx.Context()), ctx.ResponseWriter())
|
||||
return p.writeResponse(http.StatusOK, result.GetBytesBody(), requestId, ctx.ResponseWriter())
|
||||
}
|
||||
|
||||
func (p Grpc) handleError(err error, requestId string, w http.ResponseWriter) error {
|
||||
func (p Grpc) handleError(err error, w http.ResponseWriter) error {
|
||||
status, ok := status.FromError(err)
|
||||
if !ok {
|
||||
return httperrors.New(
|
||||
@@ -118,12 +118,12 @@ func (p Grpc) handleError(err error, requestId string, w http.ResponseWriter) er
|
||||
switch {
|
||||
case typeOfDetail.GetBytesBody() != nil:
|
||||
case typeOfDetail.GetListBody() != nil:
|
||||
return p.writeProto(statusCode, typeOfDetail.GetListBody(), requestId, w)
|
||||
return p.writeProto(statusCode, typeOfDetail.GetListBody(), w)
|
||||
case typeOfDetail.GetStructBody() != nil:
|
||||
return p.writeProto(statusCode, typeOfDetail.GetStructBody(), requestId, w)
|
||||
return p.writeProto(statusCode, typeOfDetail.GetStructBody(), w)
|
||||
}
|
||||
default:
|
||||
return p.writeProto(statusCode, typeOfDetail, requestId, w)
|
||||
return p.writeProto(statusCode, typeOfDetail, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,17 +134,19 @@ func (p Grpc) handleError(err error, requestId string, w http.ResponseWriter) er
|
||||
)
|
||||
}
|
||||
|
||||
func (p Grpc) writeProto(statusCode int, proto interface{}, requestId string, w http.ResponseWriter) error {
|
||||
func (p Grpc) writeProto(statusCode int, proto interface{}, w http.ResponseWriter) error {
|
||||
data, err := json.Marshal(proto)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "marshal grpc details to json")
|
||||
}
|
||||
return p.writeResponse(statusCode, data, requestId, w)
|
||||
return p.writeResponse(statusCode, data, "", w)
|
||||
}
|
||||
|
||||
func (p Grpc) writeResponse(statusCode int, data []byte, requestId string, w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set(requestid.Header, requestId)
|
||||
if requestId != "" {
|
||||
w.Header().Set(requestid.Header, requestId)
|
||||
}
|
||||
w.WriteHeader(statusCode)
|
||||
_, err := w.Write(data)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user