package awsjob import ( "errors" "fmt" "strings" "amadan.net/oficina/equipo/internal/isolatedjob" "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms/types" "github.com/aws/smithy-go" ) // mapErr wraps an error from operation op in the isolatedjob sentinel it // means, keeping the provider error too: // // - ResourceNotFoundException: ErrNotFound (Stop turns it into success) // - ThrottlingException, TooManyRequestsException, // ServiceQuotaExceededException: ErrThrottled // - ValidationException on run whose message says the client token was // used with different request parameters: ErrConflict. One attempt is // one VM, so a changed spec under the same attempt conflicts, as with // the fake. Task 17 attempt 4 saw this text live; other // ValidationExceptions, the payload limit included, keep ErrInvalid. // - ValidationException, or a request the SDK refused before sending: // ErrInvalid // - ConflictException: ErrConflict (Resume reads the VM first) // // Anything else is wrapped with no sentinel. func mapErr(op string, err error) error { if err == nil { return nil } var ( notFound *types.ResourceNotFoundException throttle *types.ThrottlingException tooMany *types.TooManyRequestsException quota *types.ServiceQuotaExceededException validation *types.ValidationException params *smithy.InvalidParamsError conflict *types.ConflictException ) var sentinel error switch { case op == "run" && errors.As(err, &validation) && clientTokenReused(validation): sentinel = isolatedjob.ErrConflict case errors.As(err, ¬Found): sentinel = isolatedjob.ErrNotFound case errors.As(err, &throttle), errors.As(err, &tooMany), errors.As(err, "a): sentinel = isolatedjob.ErrThrottled case errors.As(err, &validation), errors.As(err, ¶ms): sentinel = isolatedjob.ErrInvalid case errors.As(err, &conflict): sentinel = isolatedjob.ErrConflict default: return fmt.Errorf("awsjob: %s: %w", op, err) } return fmt.Errorf("awsjob: %s: %w: %w", op, sentinel, err) } // tokenReusedText is the message RunMicrovm gives for a client token reused // with different request parameters ("The provided clientToken was used // with different request parameters.", Task 17 attempt 4). const tokenReusedText = "clientToken was used with different request parameters" func clientTokenReused(v *types.ValidationException) bool { return strings.Contains(v.ErrorMessage(), tokenReusedText) }