go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/api/endpoints/coordinator/services/v1/util.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package logdog 16 17 import ( 18 "go.chromium.org/luci/common/retry/transient" 19 "go.chromium.org/luci/grpc/grpcutil" 20 21 "google.golang.org/grpc/codes" 22 "google.golang.org/grpc/status" 23 ) 24 25 // GetMessageProject implements ProjectBoundMessage. 26 func (ar *RegisterStreamRequest) GetMessageProject() string { return ar.Project } 27 28 // GetMessageProject implements ProjectBoundMessage. 29 func (ar *LoadStreamRequest) GetMessageProject() string { return ar.Project } 30 31 // GetMessageProject implements ProjectBoundMessage. 32 func (ar *TerminateStreamRequest) GetMessageProject() string { return ar.Project } 33 34 // GetMessageProject implements ProjectBoundMessage. 35 func (ar *ArchiveStreamRequest) GetMessageProject() string { return ar.Project } 36 37 // Complete returns true if the archive request expresses that the archived 38 // log stream was complete. 39 // 40 // A log stream is complete if every entry between zero and its terminal index 41 // is included. 42 func (ar *ArchiveStreamRequest) Complete() bool { 43 tidx := ar.TerminalIndex 44 if tidx < 0 { 45 tidx = -1 46 } 47 return (ar.LogEntryCount == (tidx + 1)) 48 } 49 50 // MakeError returns an Error object for err to return as part of RPC response. 51 // 52 // If err is a wrapped gRPC error, its code will be extracted and embedded in 53 // the returned Error. 54 // 55 // Following codes are considered transient: codes.Internal, codes.Unknown, 56 // codes.Unavailable, codes.DeadlineExceeded. Additionally any error tagged with 57 // transient.Tag is also considered transient. 58 // 59 // The Msg field will not be populated. 60 func MakeError(err error) *Error { 61 if err == nil { 62 return nil 63 } 64 code := grpcutil.Code(err) 65 return &Error{ 66 GrpcCode: int32(code), 67 Transient: transient.Tag.In(err) || grpcutil.IsTransientCode(code) || code == codes.DeadlineExceeded, 68 } 69 } 70 71 // ToError converts an Error into a gRPC error. If e is nil, a nil error will 72 // be returned. 73 func (e *Error) ToError() error { 74 if e == nil { 75 return nil 76 } 77 78 code := codes.Code(e.GrpcCode) 79 err := status.Errorf(code, "%s", e.Msg) 80 if e.Transient || grpcutil.IsTransientCode(code) { 81 err = transient.Tag.Apply(err) 82 } 83 return err 84 }