Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Grzegorz Burzyński <czeslavo@gmail.com>
  • Loading branch information
pmalek and czeslavo authored Oct 24, 2024
1 parent b079ff4 commit 425cfaf
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions controller/konnect/ops/ops_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func SDKErrorIsConflict(sdkError *sdkkonnecterrs.SDKError) bool {

// handleUpdateError handles errors that occur during an update operation.
// If the entity is not found, then it uses the provided create function to
// recreate the it.
// recreate it.
func handleUpdateError[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
Expand All @@ -141,41 +141,25 @@ func handleUpdateError[
ent TEnt,
createFunc func(ctx context.Context) error,
) error {
var (
sdkError *sdkkonnecterrs.SDKError
id = ent.GetKonnectStatus().GetKonnectID()
)
if errors.As(err, &sdkError) {
switch sdkError.StatusCode {
case http.StatusNotFound:
logEntityNotFoundRecreating(ctx, ent, id)
if err := createFunc(ctx); err != nil {
return FailedKonnectOpError[T]{
Op: UpdateOp,
Err: err,
}
}
return nil
default:
return FailedKonnectOpError[T]{
Op: UpdateOp,
Err: sdkError,
}
}
}

var notFoundError *sdkkonnecterrs.NotFoundError
if errors.As(err, &notFoundError) {
isNotFound := func() bool {
var (
notFoundError *sdkkonnecterrs.NotFoundError
sdkError *sdkkonnecterrs.SDKError
)
return errors.As(err, &notFoundError) ||
errors.As(err, &sdkError) && sdkError.StatusCode == http.StatusNotFound
}()

if isNotFound {
id := ent.GetKonnectStatus().GetKonnectID()
logEntityNotFoundRecreating(ctx, ent, id)
if err := createFunc(ctx); err != nil {
if createErr := createFunc(ctx); createErr != nil {
return FailedKonnectOpError[T]{
Op: UpdateOp,
Err: err,
Op: CreateOp,
Err: fmt.Errorf("failed to create %s %s: %w", ent.GetTypeName(), id, createErr),
}
}
return nil
}

return FailedKonnectOpError[T]{
Op: UpdateOp,
Err: err,
Expand Down

0 comments on commit 425cfaf

Please sign in to comment.