go.temporal.io/server@v1.23.0/common/rpc/interceptor/stream_error.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package interceptor 26 27 import ( 28 "context" 29 "io" 30 31 "go.temporal.io/api/serviceerror" 32 "google.golang.org/grpc" 33 "google.golang.org/grpc/codes" 34 "google.golang.org/grpc/status" 35 ) 36 37 type ( 38 ClientStreamErrorInterceptor struct { 39 grpc.ClientStream 40 } 41 ) 42 43 var _ grpc.ClientStream = (*ClientStreamErrorInterceptor)(nil) 44 45 func NewClientStreamErrorInterceptor( 46 clientStream grpc.ClientStream, 47 ) *ClientStreamErrorInterceptor { 48 return &ClientStreamErrorInterceptor{ 49 ClientStream: clientStream, 50 } 51 } 52 53 func (c *ClientStreamErrorInterceptor) CloseSend() error { 54 return errorConvert(c.ClientStream.CloseSend()) 55 } 56 57 func (c *ClientStreamErrorInterceptor) SendMsg(m interface{}) error { 58 return errorConvert(c.ClientStream.SendMsg(m)) 59 } 60 61 func (c *ClientStreamErrorInterceptor) RecvMsg(m interface{}) error { 62 return errorConvert(c.ClientStream.RecvMsg(m)) 63 } 64 65 func StreamErrorInterceptor( 66 ctx context.Context, 67 desc *grpc.StreamDesc, 68 cc *grpc.ClientConn, 69 method string, 70 streamer grpc.Streamer, 71 opts ...grpc.CallOption, 72 ) (grpc.ClientStream, error) { 73 clientStream, err := streamer(ctx, desc, cc, method, opts...) 74 if err != nil { 75 return nil, errorConvert(err) 76 } 77 return NewClientStreamErrorInterceptor(clientStream), nil 78 } 79 80 func errorConvert(err error) error { 81 switch err { 82 case nil: 83 return nil 84 case io.EOF: 85 return io.EOF 86 default: 87 return FromStatus(status.Convert(err)) 88 } 89 } 90 91 // FromStatus converts gRPC Status to service error. 92 func FromStatus(st *status.Status) error { 93 if st == nil { 94 return nil 95 } 96 97 switch st.Code() { 98 case codes.OK: 99 return nil 100 case codes.DeadlineExceeded: 101 return serviceerror.NewDeadlineExceeded(st.Message()) 102 case codes.Canceled: 103 return serviceerror.NewCanceled(st.Message()) 104 case codes.InvalidArgument: 105 return serviceerror.NewInvalidArgument(st.Message()) 106 case codes.FailedPrecondition: 107 return serviceerror.NewFailedPrecondition(st.Message()) 108 case codes.Unavailable: 109 return serviceerror.NewUnavailable(st.Message()) 110 case codes.Internal: 111 return serviceerror.NewInternal(st.Message()) 112 case codes.Unknown: 113 return serviceerror.NewInternal(st.Message()) 114 default: 115 return serviceerror.NewInternal(st.Message()) 116 } 117 }