init
This commit is contained in:
1 parent
7014f56cbb
commit
fe6f1f5e5a
25 files changed
+1918
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/service"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type grpcProxy struct {
|
||||
client *backend.RxGrpcClient
|
||||
}
|
||||
|
||||
func NewProxy() grpcProxy {
|
||||
return grpcProxy{client: backend.NewRxGrpcClient(
|
||||
backend.WithDialOptions(
|
||||
grpc.WithInsecure(), grpc.WithBlock(),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(int(conf.DefaultMaxResponseBodySize))),
|
||||
),
|
||||
backend.WithDialingErrorHandler(func(err error) {
|
||||
log.Errorf(log_code.ErrorClientGrpc, "dialing err: %v", err)
|
||||
}),
|
||||
)}
|
||||
}
|
||||
|
||||
func (p grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) {
|
||||
currentTime := time.Now()
|
||||
|
||||
uri := string(ctx.RequestURI())
|
||||
SetHandler(ctx).Complete(ctx, uri, p.client)
|
||||
|
||||
executionTime := time.Since(currentTime) / 1e6
|
||||
|
||||
service.Metrics.UpdateStatusCounter(ctx.Response.StatusCode())
|
||||
if ctx.Response.StatusCode() == http.StatusOK {
|
||||
service.Metrics.UpdateResponseTime(executionTime)
|
||||
service.Metrics.UpdateMethodResponseTime(uri, executionTime)
|
||||
}
|
||||
}
|
||||
|
||||
func (p grpcProxy) Consumer(addr []structure.AddressConfiguration) bool {
|
||||
return p.client.ReceiveAddressList(addr)
|
||||
}
|
||||
|
||||
func (p grpcProxy) Close() {
|
||||
p.client.Close()
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
u "github.com/integration-system/isp-lib/utils"
|
||||
"github.com/valyala/fasthttp"
|
||||
"isp-gate-service/proxy/grpc/handlers"
|
||||
"isp-gate-service/proxy/grpc/utils"
|
||||
"mime"
|
||||
)
|
||||
|
||||
type Handler interface {
|
||||
Complete(ctx *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient)
|
||||
}
|
||||
|
||||
func SetHandler(ctx *fasthttp.RequestCtx) Handler {
|
||||
isMultipart := isMultipart(ctx)
|
||||
isExpectFile := string(ctx.Request.Header.Peek(u.ExpectFileHeader)) == "true"
|
||||
|
||||
if isMultipart {
|
||||
ctx.Response.Header.SetContentType(utils.JsonContentType)
|
||||
return handlers.SendMultipartData
|
||||
} else if isExpectFile {
|
||||
return handlers.GetFile
|
||||
} else {
|
||||
ctx.Response.Header.SetContentType(utils.JsonContentType)
|
||||
return handlers.Json
|
||||
}
|
||||
}
|
||||
|
||||
func isMultipart(ctx *fasthttp.RequestCtx) bool {
|
||||
if !ctx.IsPost() {
|
||||
return false
|
||||
}
|
||||
v := string(ctx.Request.Header.ContentType())
|
||||
if v == "" {
|
||||
return false
|
||||
}
|
||||
d, params, err := mime.ParseMediaType(v)
|
||||
if err != nil || d != "multipart/form-data" {
|
||||
return false
|
||||
}
|
||||
_, ok := params["boundary"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
isp "github.com/integration-system/isp-lib/proto/stubs"
|
||||
s "github.com/integration-system/isp-lib/streaming"
|
||||
"github.com/valyala/fasthttp"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"io"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/grpc/utils"
|
||||
"mime/multipart"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
headerKeyContentDisposition = "Content-Disposition"
|
||||
headerKeyContentType = "Content-Type"
|
||||
headerKeyContentLength = "Content-Length"
|
||||
headerKeyTransferEncoding = "Transfer-Encoding"
|
||||
|
||||
errorMsgInternal = "Internal server error"
|
||||
errorMsgInvalidArg = "Not able to read request body"
|
||||
)
|
||||
|
||||
func openStream(headers *fasthttp.RequestHeader, method string, timeout time.Duration, client *backend.RxGrpcClient) (
|
||||
isp.BackendService_RequestStreamClient, context.CancelFunc, error) {
|
||||
|
||||
cli, err := client.Conn()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
md, _ := utils.MakeMetadata(headers, method)
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), md)
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
stream, err := cli.RequestStream(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return stream, cancel, nil
|
||||
}
|
||||
|
||||
func transferFile(f multipart.File, stream isp.BackendService_RequestStreamClient,
|
||||
buffer []byte, ctx *fasthttp.RequestCtx) (bool, bool) {
|
||||
|
||||
ok := true
|
||||
eof := false
|
||||
for {
|
||||
n, err := f.Read(buffer)
|
||||
if n > 0 {
|
||||
err = stream.Send(&isp.Message{Body: &isp.Message_BytesBody{buffer[:n]}})
|
||||
if ok, eof = checkError(err, ctx); !ok || eof {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if ok, eof = checkError(err, ctx); ok && eof {
|
||||
err = stream.Send(s.FileEnd())
|
||||
ok, eof = checkError(err, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return ok, eof
|
||||
}
|
||||
|
||||
func checkError(err error, ctx *fasthttp.RequestCtx) (bool, bool) {
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.GetFile, "", err)
|
||||
utils.SendError(errorMsgInternal, codes.Internal, []interface{}{err.Error()}, ctx)
|
||||
return false, false
|
||||
}
|
||||
return true, true
|
||||
}
|
||||
return true, false
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
s "github.com/integration-system/isp-lib/streaming"
|
||||
u "github.com/integration-system/isp-lib/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/codes"
|
||||
"io"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/grpc/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var GetFile getFile
|
||||
|
||||
type getFile struct{}
|
||||
|
||||
func (getFile) Complete(ctx *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
timeout := cfg.GetStreamInvokeTimeout()
|
||||
|
||||
req, err := utils.ReadJsonBody(ctx)
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
utils.SendError(err.Error(), codes.InvalidArgument, nil, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
stream, cancel, err := openStream(&ctx.Request.Header, method, timeout, client)
|
||||
defer func() {
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
utils.SendError(errorMsgInternal, codes.Internal, []interface{}{err.Error()}, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
if req != nil {
|
||||
value := u.ConvertInterfaceToGrpcStruct(req)
|
||||
err := stream.Send(backend.WrapBody(value))
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
utils.SendError(errorMsgInternal, codes.Internal, []interface{}{err.Error()}, ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
msg, err := stream.Recv()
|
||||
if err != nil {
|
||||
bytes, status, err := utils.GetResponse(nil, err)
|
||||
if err == nil {
|
||||
ctx.SetStatusCode(status)
|
||||
ctx.SetBody(bytes)
|
||||
}
|
||||
return
|
||||
}
|
||||
bf := s.BeginFile{}
|
||||
err = bf.FromMessage(msg)
|
||||
if err != nil {
|
||||
bytes, status, err := utils.GetResponse(nil, err)
|
||||
if err == nil {
|
||||
ctx.SetStatusCode(status)
|
||||
ctx.SetBody(bytes)
|
||||
}
|
||||
return
|
||||
}
|
||||
header := &ctx.Response.Header
|
||||
header.Set(headerKeyContentDisposition, fmt.Sprintf("attachment; filename=%s", bf.FileName))
|
||||
header.Set(headerKeyContentType, bf.ContentType)
|
||||
if bf.ContentLength > 0 {
|
||||
header.Set(headerKeyContentLength, strconv.Itoa(int(bf.ContentLength)))
|
||||
} else {
|
||||
header.Set(headerKeyTransferEncoding, "chunked")
|
||||
}
|
||||
|
||||
for {
|
||||
msg, err := stream.Recv()
|
||||
if s.IsEndOfFile(msg) || err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
break
|
||||
}
|
||||
bytes := msg.GetBytesBody()
|
||||
if bytes == nil {
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
log_code.MdTypeData: log_code.TypeData.GetFile,
|
||||
log_code.MdMethod: method,
|
||||
}).Errorf(log_code.WarnRequestHandler, "Method %s. Expected bytes array", method)
|
||||
break
|
||||
}
|
||||
_, err = ctx.Write(bytes)
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
isp "github.com/integration-system/isp-lib/proto/stubs"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/journal"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/grpc/utils"
|
||||
"isp-gate-service/service"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Json handleJson
|
||||
|
||||
type handleJson struct{}
|
||||
|
||||
func (p handleJson) Complete(c *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) {
|
||||
//body, err := utils.ReadJsonBody(c)
|
||||
body := c.Request.Body()
|
||||
/*if err != nil {
|
||||
streaming.LogError(log_code.TypeData.JsonContent, method, err)
|
||||
streaming.SendError(err.Error(), codes.InvalidArgument, []interface{}{err.Error()}, c)
|
||||
return
|
||||
}*/
|
||||
|
||||
md, methodName := utils.MakeMetadata(&c.Request.Header, method)
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), md)
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
ctx, cancel := context.WithTimeout(ctx, cfg.GetSyncInvokeTimeout())
|
||||
defer cancel()
|
||||
|
||||
cli, err := client.Conn()
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.JsonContent, methodName, err)
|
||||
utils.SendError(errorMsgInternal, codes.Internal, []interface{}{err.Error()}, c)
|
||||
return
|
||||
}
|
||||
|
||||
//structBody := u.ConvertInterfaceToGrpcStruct(body)
|
||||
currentTime := time.Now()
|
||||
response, invokerErr := cli.Request(
|
||||
ctx,
|
||||
&isp.Message{
|
||||
Body: &isp.Message_BytesBody{BytesBody: body},
|
||||
},
|
||||
)
|
||||
service.Metrics.UpdateRouterResponseTime(time.Since(currentTime) / 1e6)
|
||||
|
||||
if data, status, err := utils.GetResponse(response, invokerErr); err == nil {
|
||||
c.SetStatusCode(status)
|
||||
_, _ = c.Write(data)
|
||||
if cfg.Journal.Enable && service.JournalMethodsMatcher.Match(methodName) {
|
||||
if invokerErr != nil {
|
||||
if err := journal.Client.Error(methodName, body, data, invokerErr); err != nil {
|
||||
log.Warnf(log_code.WarnJournalCouldNotWriteToFile, "could not write to file journal: %v", err)
|
||||
}
|
||||
} else {
|
||||
if err := journal.Client.Info(methodName, body, data); err != nil {
|
||||
log.Warnf(log_code.WarnJournalCouldNotWriteToFile, "could not write to file journal: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.JsonContent, methodName, err)
|
||||
utils.SendError(errorMsgInternal, codes.Internal, []interface{}{err.Error()}, c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
s "github.com/integration-system/isp-lib/streaming"
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/grpc/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var SendMultipartData sendMultipartData
|
||||
|
||||
type sendMultipartData struct{}
|
||||
|
||||
func (sendMultipartData) Complete(ctx *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
timeout := cfg.GetStreamInvokeTimeout()
|
||||
bufferSize := cfg.GetTransferFileBufferSize()
|
||||
|
||||
stream, cancel, err := openStream(&ctx.Request.Header, method, timeout, client)
|
||||
defer func() {
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.SendMultipart, method, err)
|
||||
utils.SendError(errorMsgInternal, codes.Internal, []interface{}{err.Error()}, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
form, err := ctx.MultipartForm()
|
||||
defer func() {
|
||||
if form != nil {
|
||||
_ = form.RemoveAll()
|
||||
}
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.SendMultipart, method, err)
|
||||
utils.SendError(errorMsgInvalidArg, codes.InvalidArgument, []interface{}{err.Error()}, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
formData := make(map[string]interface{}, len(form.Value))
|
||||
|
||||
for k, v := range form.Value {
|
||||
if len(v) > 0 {
|
||||
formData[k] = v[0]
|
||||
}
|
||||
}
|
||||
|
||||
response := make([]string, 0)
|
||||
buffer := make([]byte, bufferSize)
|
||||
ok := true
|
||||
eof := false
|
||||
|
||||
for formDataName, files := range form.File {
|
||||
if len(files) == 0 {
|
||||
continue
|
||||
}
|
||||
file := files[0]
|
||||
fileName := file.Filename
|
||||
contentType := file.Header.Get(headerKeyContentType)
|
||||
contentLength := file.Size
|
||||
bf := s.BeginFile{
|
||||
FileName: fileName,
|
||||
FormDataName: formDataName,
|
||||
ContentType: contentType,
|
||||
ContentLength: contentLength,
|
||||
FormData: formData,
|
||||
}
|
||||
err = stream.Send(bf.ToMessage())
|
||||
if ok, eof = checkError(err, ctx); !ok || eof {
|
||||
break
|
||||
}
|
||||
|
||||
f, err := file.Open()
|
||||
if ok, eof = checkError(err, ctx); !ok || eof {
|
||||
break
|
||||
}
|
||||
if ok, eof = transferFile(f, stream, buffer, ctx); ok {
|
||||
msg, err := stream.Recv()
|
||||
v, _, err := utils.GetResponse(msg, err)
|
||||
if err == nil {
|
||||
response = append(response, string(v))
|
||||
}
|
||||
ok = err == nil
|
||||
}
|
||||
|
||||
if !ok || eof {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
err = stream.CloseSend()
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.SendMultipart, method, err)
|
||||
}
|
||||
|
||||
if ok {
|
||||
arrayBody := strings.Join(response, ",")
|
||||
_, err = ctx.WriteString("[" + arrayBody + "]")
|
||||
if err != nil {
|
||||
utils.LogRequestHandlerError(log_code.TypeData.SendMultipart, method, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/log_code"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/ptypes/struct"
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
http2 "github.com/integration-system/isp-lib/http"
|
||||
"github.com/integration-system/isp-lib/proto/stubs"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
const (
|
||||
JsonContentType = "application/json; charset=utf-8"
|
||||
)
|
||||
|
||||
var (
|
||||
json = jsoniter.ConfigFastest
|
||||
)
|
||||
|
||||
func ReadJsonBody(ctx *fasthttp.RequestCtx) (interface{}, error) {
|
||||
requestBody := ctx.Request.Body()
|
||||
var body interface{}
|
||||
if len(requestBody) == 0 {
|
||||
requestBody = []byte("{}")
|
||||
}
|
||||
if requestBody[0] == '{' {
|
||||
body = make(map[string]interface{})
|
||||
} else if requestBody[0] == '[' {
|
||||
body = make([]interface{}, 0)
|
||||
} else {
|
||||
return nil, errors.New("Invalid json format. Expected object or array")
|
||||
}
|
||||
|
||||
err := json.Unmarshal(requestBody, &body)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.New("Not able to read request body")
|
||||
}
|
||||
return body, err
|
||||
}
|
||||
|
||||
func GetResponse(msg *isp.Message, err error) ([]byte, int, error) {
|
||||
if err != nil {
|
||||
errorBody, errorStatus := convertError(err)
|
||||
return errorBody, errorStatus, nil
|
||||
}
|
||||
|
||||
bytes := msg.GetBytesBody()
|
||||
if bytes != nil {
|
||||
return bytes, http.StatusOK, nil
|
||||
}
|
||||
result := backend.ResolveBody(msg)
|
||||
data := utils.ConvertGrpcStructToInterface(result)
|
||||
byteResponse, err := json.Marshal(data)
|
||||
return byteResponse, http.StatusOK, err
|
||||
}
|
||||
|
||||
func MakeMetadata(r *fasthttp.RequestHeader, method string) (metadata.MD, string) {
|
||||
method = strings.TrimPrefix(method, "/api/")
|
||||
md := metadata.Pairs(utils.ProxyMethodNameHeader, method)
|
||||
r.VisitAll(func(key, v []byte) {
|
||||
lowerHeader := strings.ToLower(string(key))
|
||||
if len(v) > 0 && strings.HasPrefix(lowerHeader, "x-") {
|
||||
md = metadata.Join(md, metadata.Pairs(lowerHeader, string(v)))
|
||||
}
|
||||
})
|
||||
return md, method
|
||||
}
|
||||
|
||||
func LogRequestHandlerError(typeData, method string, err error) {
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
log_code.MdTypeData: typeData,
|
||||
log_code.MdMethod: method,
|
||||
}).Warn(log_code.WarnRequestHandler, err)
|
||||
}
|
||||
|
||||
func SendError(errorMessage string, errorCode codes.Code, details []interface{}, ctx *fasthttp.RequestCtx) {
|
||||
grpcCode := errorCode.String()
|
||||
|
||||
structureError := structure.GrpcError{
|
||||
ErrorMessage: errorMessage,
|
||||
ErrorCode: grpcCode,
|
||||
Details: details,
|
||||
}
|
||||
|
||||
ctx.SetStatusCode(http2.CodeToHttpStatus(errorCode))
|
||||
msg, _ := json.Marshal(structureError)
|
||||
_, _ = ctx.Write(msg)
|
||||
}
|
||||
|
||||
func convertError(err error) ([]byte, int) {
|
||||
s, ok := status.FromError(err)
|
||||
if ok {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
if cfg.EnableOriginalProtoErrors {
|
||||
if body, err := proto.Marshal(s.Proto()); err != nil {
|
||||
return []byte(utils.ServiceError), http.StatusServiceUnavailable
|
||||
} else {
|
||||
return body, http2.CodeToHttpStatus(s.Code())
|
||||
}
|
||||
} else {
|
||||
details := s.Details()
|
||||
newDetails := make([]interface{}, len(details))
|
||||
for i, detail := range details {
|
||||
switch typeOfDetail := detail.(type) {
|
||||
case *structpb.Struct:
|
||||
newDetails[i] = utils.ConvertGrpcStructToInterface(
|
||||
&structpb.Value{Kind: &structpb.Value_StructValue{StructValue: typeOfDetail}},
|
||||
)
|
||||
case *isp.Message:
|
||||
newDetails[i] = utils.ConvertGrpcStructToInterface(
|
||||
backend.ResolveBody(typeOfDetail),
|
||||
)
|
||||
default:
|
||||
newDetails[i] = typeOfDetail
|
||||
}
|
||||
}
|
||||
|
||||
var respBody interface{}
|
||||
if cfg.ProxyGrpcErrorDetails && len(newDetails) > 0 {
|
||||
respBody = newDetails[0]
|
||||
} else {
|
||||
respBody = structure.GrpcError{ErrorMessage: s.Message(), ErrorCode: s.Code().String(), Details: newDetails}
|
||||
}
|
||||
if errorData, err := json.Marshal(respBody); err != nil {
|
||||
log.Warn(log_code.WarnConvertErrorDataMarshalResponse, err)
|
||||
return []byte(utils.ServiceError), http.StatusServiceUnavailable
|
||||
} else {
|
||||
return errorData, http2.CodeToHttpStatus(s.Code())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return []byte(utils.ServiceError), http.StatusServiceUnavailable
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user