[backend/filestate] Don't unwrap go-cloud errors (#8455)

* Don't unwrap go-cloud errors

* Remove unused func

* Add changelog entry

Co-authored-by: Ian Wahbe <ian@wahbe.com>
This commit is contained in:
Praneet Loke 2021-11-19 12:21:37 -08:00 committed by GitHub
parent f98d39b84b
commit 2c25c3fbd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 12 deletions

View file

@ -16,3 +16,6 @@
- [codegen/typescript] - Respect default values in Pulumi object types.
[#8400](https://github.com/pulumi/pulumi/pull/8400)
- [cli] - Catch expected errors in filestate backend stacks.
[#8455](https://github.com/pulumi/pulumi/pull/8455)

View file

@ -323,7 +323,7 @@ func (b *localBackend) GetStack(ctx context.Context, stackRef backend.StackRefer
snapshot, path, err := b.getStack(stackName)
switch {
case gcerrors.Code(drillError(err)) == gcerrors.NotFound:
case gcerrors.Code(err) == gcerrors.NotFound:
return nil, nil
case err != nil:
return nil, err
@ -882,12 +882,3 @@ func (b *localBackend) UpdateStackTags(ctx context.Context,
// The local backend does not currently persist tags.
return errors.New("stack tags not supported in --local mode")
}
// Returns the original error in the chain. If `err` is nil, nil is returned.
func drillError(err error) error {
e := err
for errors.Unwrap(e) != nil {
e = errors.Unwrap(e)
}
return e
}

View file

@ -187,3 +187,20 @@ func TestListStacksWithMultiplePassphrases(t *testing.T) {
}
}
func TestDrillError(t *testing.T) {
// Login to a temp dir filestate backend
tmpDir, err := ioutil.TempDir("", "filestatebackend")
assert.NoError(t, err)
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()
// Get a non-existent stack and expect a nil error because it won't be found.
stackRef, err := b.ParseStackReference("dev")
if err != nil {
t.Fatalf("unexpected error %v when parsing stack reference", err)
}
_, err = b.GetStack(ctx, stackRef)
assert.Nil(t, err)
}

View file

@ -323,7 +323,7 @@ func (b *localBackend) getHistory(name tokens.QName, pageSize int, page int) ([]
allFiles, err := listBucket(b.bucket, dir)
if err != nil {
// History doesn't exist until a stack has been updated.
if gcerrors.Code(drillError(err)) == gcerrors.NotFound {
if gcerrors.Code(err) == gcerrors.NotFound {
return nil, nil
}
return nil, err
@ -391,7 +391,7 @@ func (b *localBackend) renameHistory(oldName tokens.QName, newName tokens.QName)
allFiles, err := listBucket(b.bucket, oldHistory)
if err != nil {
// if there's nothing there, we don't really need to do a rename.
if gcerrors.Code(drillError(err)) == gcerrors.NotFound {
if gcerrors.Code(err) == gcerrors.NotFound {
return nil
}
return err