corso/src/pkg/errs/errs.go
Keepers 7196b5d278
catch and sentinel resource locked error case (#4465)
handle cases where a resource is found, but is not accessible due to being locked out by an administrator or msoft process.

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included

#### Type of change

- [x] 🐛 Bugfix

#### Issue(s)

* #4464

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
2023-10-10 19:24:48 +00:00

57 lines
1.7 KiB
Go

package errs
import (
"errors"
"github.com/alcionai/corso/src/internal/m365/graph"
"github.com/alcionai/corso/src/pkg/repository"
)
// expose enums, rather than errors, for Is checks. The enum should
// map to a specific internal error that can be used for the actual
// errors.Is comparison.
type errEnum string
const (
ApplicationThrottled errEnum = "application-throttled"
BackupNotFound errEnum = "backup-not-found"
RepoAlreadyExists errEnum = "repository-already-exists"
ResourceNotAccessible errEnum = "resource-not-accesible"
ResourceOwnerNotFound errEnum = "resource-owner-not-found"
ServiceNotEnabled errEnum = "service-not-enabled"
)
// map of enums to errors. We might want to re-use an enum for multiple
// internal errors (ex: "ServiceNotEnabled" may exist in both graph and
// non-graph producers).
var internalToExternal = map[errEnum][]error{
ApplicationThrottled: {graph.ErrApplicationThrottled},
BackupNotFound: {repository.ErrorBackupNotFound},
RepoAlreadyExists: {repository.ErrorRepoAlreadyExists},
ResourceNotAccessible: {graph.ErrResourceLocked},
ResourceOwnerNotFound: {graph.ErrResourceOwnerNotFound},
ServiceNotEnabled: {graph.ErrServiceNotEnabled},
}
// Internal returns the internal errors which match to the public error category.
func Internal(enum errEnum) []error {
return internalToExternal[enum]
}
// Is checks if the provided error contains an internal error that matches
// the public error category.
func Is(err error, enum errEnum) bool {
internalErrs, ok := internalToExternal[enum]
if !ok {
return false
}
for _, target := range internalErrs {
if errors.Is(err, target) {
return true
}
}
return false
}