[#142] Fix unwrapErr for go 1.20

Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
This commit is contained in:
Alejandro Lopez 2023-08-04 11:43:36 +03:00
parent 3dc8129ed7
commit f5cdd00f27

View file

@ -1,85 +1,64 @@
package client package client
import ( import (
"errors"
"fmt" "fmt"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status" apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
) )
// unwraps err using errors.Unwrap and returns the result. // wrapsErrType returns true if any error in the error tree of err is of type E.
func unwrapErr(err error) error { func wrapsErrType[E error](err error) bool {
for e := errors.Unwrap(err); e != nil; e = errors.Unwrap(err) { switch e := err.(type) {
err = e case E:
return true
case interface{ Unwrap() error }:
return wrapsErrType[E](e.Unwrap())
case interface{ Unwrap() []error }:
for _, ei := range e.Unwrap() {
if wrapsErrType[E](ei) {
return true
}
}
return false
default:
return false
} }
return err
} }
// IsErrContainerNotFound checks if err corresponds to FrostFS status // IsErrContainerNotFound checks if err corresponds to FrostFS status
// return corresponding to missing container. Supports wrapped errors. // return corresponding to missing container. Supports wrapped errors.
func IsErrContainerNotFound(err error) bool { func IsErrContainerNotFound(err error) bool {
switch unwrapErr(err).(type) { return wrapsErrType[*apistatus.ContainerNotFound](err)
default:
return false
case *apistatus.ContainerNotFound:
return true
}
} }
// IsErrEACLNotFound checks if err corresponds to FrostFS status // IsErrEACLNotFound checks if err corresponds to FrostFS status
// return corresponding to missing eACL table. Supports wrapped errors. // return corresponding to missing eACL table. Supports wrapped errors.
func IsErrEACLNotFound(err error) bool { func IsErrEACLNotFound(err error) bool {
switch unwrapErr(err).(type) { return wrapsErrType[*apistatus.EACLNotFound](err)
default:
return false
case *apistatus.EACLNotFound:
return true
}
} }
// IsErrObjectNotFound checks if err corresponds to FrostFS status // IsErrObjectNotFound checks if err corresponds to FrostFS status
// return corresponding to missing object. Supports wrapped errors. // return corresponding to missing object. Supports wrapped errors.
func IsErrObjectNotFound(err error) bool { func IsErrObjectNotFound(err error) bool {
switch unwrapErr(err).(type) { return wrapsErrType[*apistatus.ObjectNotFound](err)
default:
return false
case *apistatus.ObjectNotFound:
return true
}
} }
// IsErrObjectAlreadyRemoved checks if err corresponds to FrostFS status // IsErrObjectAlreadyRemoved checks if err corresponds to FrostFS status
// return corresponding to already removed object. Supports wrapped errors. // return corresponding to already removed object. Supports wrapped errors.
func IsErrObjectAlreadyRemoved(err error) bool { func IsErrObjectAlreadyRemoved(err error) bool {
switch unwrapErr(err).(type) { return wrapsErrType[*apistatus.ObjectAlreadyRemoved](err)
default:
return false
case *apistatus.ObjectAlreadyRemoved:
return true
}
} }
// IsErrSessionExpired checks if err corresponds to FrostFS status return // IsErrSessionExpired checks if err corresponds to FrostFS status return
// corresponding to expired session. Supports wrapped errors. // corresponding to expired session. Supports wrapped errors.
func IsErrSessionExpired(err error) bool { func IsErrSessionExpired(err error) bool {
switch unwrapErr(err).(type) { return wrapsErrType[*apistatus.SessionTokenExpired](err)
default:
return false
case *apistatus.SessionTokenExpired:
return true
}
} }
// IsErrSessionNotFound checks if err corresponds to FrostFS status return // IsErrSessionNotFound checks if err corresponds to FrostFS status return
// corresponding to missing session. Supports wrapped errors. // corresponding to missing session. Supports wrapped errors.
func IsErrSessionNotFound(err error) bool { func IsErrSessionNotFound(err error) bool {
switch unwrapErr(err).(type) { return wrapsErrType[*apistatus.SessionTokenNotFound](err)
default:
return false
case *apistatus.SessionTokenNotFound:
return true
}
} }
// returns error describing missing field with the given name. // returns error describing missing field with the given name.