github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/object/status.go (about) 1 package object 2 3 import ( 4 "github.com/TrueCloudLab/frostfs-api-go/v2/status" 5 statusgrpc "github.com/TrueCloudLab/frostfs-api-go/v2/status/grpc" 6 ) 7 8 // LocalizeFailStatus checks if passed global status.Code is related to object failure and: 9 // 10 // then localizes the code and returns true, 11 // else leaves the code unchanged and returns false. 12 // 13 // Arg must not be nil. 14 func LocalizeFailStatus(c *status.Code) bool { 15 return status.LocalizeIfInSection(c, uint32(statusgrpc.Section_SECTION_OBJECT)) 16 } 17 18 // GlobalizeFail globalizes local code of object failure. 19 // 20 // Arg must not be nil. 21 func GlobalizeFail(c *status.Code) { 22 c.GlobalizeSection(uint32(statusgrpc.Section_SECTION_OBJECT)) 23 } 24 25 const ( 26 // StatusAccessDenied is a local status.Code value for 27 // ACCESS_DENIED object failure. 28 StatusAccessDenied status.Code = iota 29 // StatusNotFound is a local status.Code value for 30 // OBJECT_NOT_FOUND object failure. 31 StatusNotFound 32 // StatusLocked is a local status.Code value for 33 // LOCKED object failure. 34 StatusLocked 35 // StatusLockNonRegularObject is a local status.Code value for 36 // LOCK_NON_REGULAR_OBJECT object failure. 37 StatusLockNonRegularObject 38 // StatusAlreadyRemoved is a local status.Code value for 39 // OBJECT_ALREADY_REMOVED object failure. 40 StatusAlreadyRemoved 41 // StatusOutOfRange is a local status.Code value for 42 // OUT_OF_RANGE object failure. 43 StatusOutOfRange 44 ) 45 46 const ( 47 // detailAccessDeniedDesc is a StatusAccessDenied detail ID for 48 // human-readable description. 49 detailAccessDeniedDesc = iota 50 ) 51 52 // WriteAccessDeniedDesc writes human-readable description of StatusAccessDenied 53 // into status.Status as a detail. The status must not be nil. 54 // 55 // Existing details are expected to be ID-unique, otherwise undefined behavior. 56 func WriteAccessDeniedDesc(st *status.Status, desc string) { 57 var found bool 58 59 st.IterateDetails(func(d *status.Detail) bool { 60 if d.ID() == detailAccessDeniedDesc { 61 found = true 62 d.SetValue([]byte(desc)) 63 } 64 65 return found 66 }) 67 68 if !found { 69 var d status.Detail 70 71 d.SetID(detailAccessDeniedDesc) 72 d.SetValue([]byte(desc)) 73 74 st.AppendDetails(d) 75 } 76 } 77 78 // ReadAccessDeniedDesc looks up for status detail with human-readable description 79 // of StatusAccessDenied. Returns empty string if detail is missing. 80 func ReadAccessDeniedDesc(st status.Status) (desc string) { 81 st.IterateDetails(func(d *status.Detail) bool { 82 if d.ID() == detailAccessDeniedDesc { 83 desc = string(d.Value()) 84 return true 85 } 86 87 return false 88 }) 89 90 return 91 }