Skip to content

Commit e989102

Browse files
authored
fix: add error message that key pair doesn't exists (#732)
* Add error message that key pair doesn't exists when the API returns a 404 * Update error check
1 parent d443b54 commit e989102

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

stackit/internal/services/iaas/keypair/datasource.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keypair
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78

@@ -130,8 +131,14 @@ func (r *keyPairDataSource) Read(ctx context.Context, req datasource.ReadRequest
130131

131132
keypairResp, err := r.client.GetKeyPair(ctx, name).Execute()
132133
if err != nil {
133-
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
134+
var oapiErr *oapierror.GenericOpenAPIError
135+
ok := errors.As(err, &oapiErr)
134136
if ok && oapiErr.StatusCode == http.StatusNotFound {
137+
summary := fmt.Sprintf("Key Pair with name %q does not exists", name)
138+
description := fmt.Sprintf("Key Pair with name %q cannot be found. A key pair can be added with the resource \"stackit_key_pair\"", name)
139+
diags.AddError(summary, description)
140+
resp.Diagnostics.Append(diags...)
141+
135142
resp.State.RemoveResource(ctx)
136143
return
137144
}

0 commit comments

Comments
 (0)