Files
seaweedfs/seaweed-block/core/rpc/lifecycleclient/reason.go
T
pingqiuandClaude Opus 5.5 58c5a47243 fix(sw-block): typed codes for a foreign-source restore and an unknown volume
The master's refusal of a restore whose snapshot names another volume's
chunks carries a typed reason (an ErrorInfo detail, RESTORE_SOURCE_NOT_OWN)
and the CLI maps it to its existing JSON code restore_source_not_own
instead of the generic not_admitted. A volume the master does not know
(NotFound from the resolve) exits with volume_not_found. A client decides
by these codes, never by message text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 00:55:36 -08:00

44 lines
1.3 KiB
Go

package lifecycleclient
import (
"errors"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/status"
)
// Typed refusal reasons the master attaches to a gRPC status (an ErrorInfo
// detail in ErrorDomain), so a client decides by the reason, never by the
// message text.
const (
ErrorDomain = "seaweed-block"
// ReasonRestoreSourceNotOwn: a restore was refused because the snapshot
// names chunks of a volume other than its source (a restore target's
// snapshot cannot be restored again).
ReasonRestoreSourceNotOwn = "RESTORE_SOURCE_NOT_OWN"
)
// WithReason returns st with an ErrorInfo detail carrying reason in
// ErrorDomain; st unchanged if the detail cannot be attached.
func WithReason(st *status.Status, reason string) *status.Status {
if d, err := st.WithDetails(&errdetails.ErrorInfo{Reason: reason, Domain: ErrorDomain}); err == nil {
return d
}
return st
}
// Reason is the typed refusal reason in err's gRPC status (an ErrorInfo in
// ErrorDomain), or "" when it carries none. err may be wrapped.
func Reason(err error) string {
var se interface{ GRPCStatus() *status.Status }
if !errors.As(err, &se) {
return ""
}
for _, d := range se.GRPCStatus().Details() {
if info, ok := d.(*errdetails.ErrorInfo); ok && info.GetDomain() == ErrorDomain {
return info.GetReason()
}
}
return ""
}