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
This commit is contained in:
Marcel Jacek 2025-03-21 12:44:20 +01:00 committed by GitHub
parent d443b5416d
commit e989102d6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,7 @@ package keypair
import (
"context"
"errors"
"fmt"
"net/http"
@ -130,8 +131,14 @@ func (r *keyPairDataSource) Read(ctx context.Context, req datasource.ReadRequest
keypairResp, err := r.client.GetKeyPair(ctx, name).Execute()
if err != nil {
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
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
if ok && oapiErr.StatusCode == http.StatusNotFound {
summary := fmt.Sprintf("Key Pair with name %q does not exists", name)
description := fmt.Sprintf("Key Pair with name %q cannot be found. A key pair can be added with the resource \"stackit_key_pair\"", name)
diags.AddError(summary, description)
resp.Diagnostics.Append(diags...)
resp.State.RemoveResource(ctx)
return
}