frostfs-api-go/object/status.go
Airat Arifullin 2511f4ca70
All checks were successful
Tests and linters / Tests (1.19) (pull_request) Successful in 1m39s
Tests and linters / Lint (pull_request) Successful in 1m46s
Tests and linters / Tests with -race (pull_request) Successful in 3m12s
Tests and linters / Tests (1.20) (pull_request) Successful in 7m6s
[#40] types: Generate StableMarshaler/StableSize methods for protobufs
* Add plugin option for protogen in Makefile
* Fix the generator for the plugin in util/protogen
* Erase convertable types, move helpful methods to gRPC protobufs
* Erase helpers for convertations
* Generate StableMarshlal/StableSize for protobufs by the protoc plugin

Signed-off-by: Airat Arifullin a.arifullin@yadro.com
2023-07-10 15:29:21 +03:00

91 lines
2.5 KiB
Go

package object
import (
"git.frostfs.info/TrueCloudLab/aarifullin/v2/status"
statusgrpc "git.frostfs.info/TrueCloudLab/aarifullin/v2/status/grpc"
)
// LocalizeFailStatus checks if passed global status.Code is related to object failure and:
//
// then localizes the code and returns true,
// else leaves the code unchanged and returns false.
//
// Arg must not be nil.
func LocalizeFailStatus(c *status.Code) bool {
return status.LocalizeIfInSection(c, uint32(statusgrpc.Section_SECTION_OBJECT))
}
// GlobalizeFail globalizes local code of object failure.
//
// Arg must not be nil.
func GlobalizeFail(c *status.Code) {
c.GlobalizeSection(uint32(statusgrpc.Section_SECTION_OBJECT))
}
const (
// StatusAccessDenied is a local status.Code value for
// ACCESS_DENIED object failure.
StatusAccessDenied status.Code = iota
// StatusNotFound is a local status.Code value for
// OBJECT_NOT_FOUND object failure.
StatusNotFound
// StatusLocked is a local status.Code value for
// LOCKED object failure.
StatusLocked
// StatusLockNonRegularObject is a local status.Code value for
// LOCK_NON_REGULAR_OBJECT object failure.
StatusLockNonRegularObject
// StatusAlreadyRemoved is a local status.Code value for
// OBJECT_ALREADY_REMOVED object failure.
StatusAlreadyRemoved
// StatusOutOfRange is a local status.Code value for
// OUT_OF_RANGE object failure.
StatusOutOfRange
)
const (
// detailAccessDeniedDesc is a StatusAccessDenied detail ID for
// human-readable description.
detailAccessDeniedDesc = iota
)
// WriteAccessDeniedDesc writes human-readable description of StatusAccessDenied
// into status.Status as a detail. The status must not be nil.
//
// Existing details are expected to be ID-unique, otherwise undefined behavior.
func WriteAccessDeniedDesc(st *statusgrpc.Status, desc string) {
var found bool
st.IterateDetails(func(d *statusgrpc.Status_Detail) bool {
if d.GetId() == detailAccessDeniedDesc {
found = true
d.SetValue([]byte(desc))
}
return found
})
if !found {
d := new(statusgrpc.Status_Detail)
d.SetId(detailAccessDeniedDesc)
d.SetValue([]byte(desc))
st.AppendDetails(d)
}
}
// ReadAccessDeniedDesc looks up for status detail with human-readable description
// of StatusAccessDenied. Returns empty string if detail is missing.
func ReadAccessDeniedDesc(st *statusgrpc.Status) (desc string) {
st.IterateDetails(func(d *statusgrpc.Status_Detail) bool {
if d.GetId() == detailAccessDeniedDesc {
desc = string(d.GetValue())
return true
}
return false
})
return
}