| 1 | package awsjob |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 9 | "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms/types" |
| 10 | "github.com/aws/smithy-go" |
| 11 | ) |
| 12 | |
| 13 | // mapErr wraps an error from operation op in the isolatedjob sentinel it |
| 14 | // means, keeping the provider error too: |
| 15 | // |
| 16 | // - ResourceNotFoundException: ErrNotFound (Stop turns it into success) |
| 17 | // - ThrottlingException, TooManyRequestsException, |
| 18 | // ServiceQuotaExceededException: ErrThrottled |
| 19 | // - ValidationException on run whose message says the client token was |
| 20 | // used with different request parameters: ErrConflict. One attempt is |
| 21 | // one VM, so a changed spec under the same attempt conflicts, as with |
| 22 | // the fake. Task 17 attempt 4 saw this text live; other |
| 23 | // ValidationExceptions, the payload limit included, keep ErrInvalid. |
| 24 | // - ValidationException, or a request the SDK refused before sending: |
| 25 | // ErrInvalid |
| 26 | // - ConflictException: ErrConflict (Resume reads the VM first) |
| 27 | // |
| 28 | // Anything else is wrapped with no sentinel. |
| 29 | func mapErr(op string, err error) error { |
| 30 | if err == nil { |
| 31 | return nil |
| 32 | } |
| 33 | var ( |
| 34 | notFound *types.ResourceNotFoundException |
| 35 | throttle *types.ThrottlingException |
| 36 | tooMany *types.TooManyRequestsException |
| 37 | quota *types.ServiceQuotaExceededException |
| 38 | validation *types.ValidationException |
| 39 | params *smithy.InvalidParamsError |
| 40 | conflict *types.ConflictException |
| 41 | ) |
| 42 | var sentinel error |
| 43 | switch { |
| 44 | case op == "run" && errors.As(err, &validation) && clientTokenReused(validation): |
| 45 | sentinel = isolatedjob.ErrConflict |
| 46 | case errors.As(err, ¬Found): |
| 47 | sentinel = isolatedjob.ErrNotFound |
| 48 | case errors.As(err, &throttle), errors.As(err, &tooMany), errors.As(err, "a): |
| 49 | sentinel = isolatedjob.ErrThrottled |
| 50 | case errors.As(err, &validation), errors.As(err, ¶ms): |
| 51 | sentinel = isolatedjob.ErrInvalid |
| 52 | case errors.As(err, &conflict): |
| 53 | sentinel = isolatedjob.ErrConflict |
| 54 | default: |
| 55 | return fmt.Errorf("awsjob: %s: %w", op, err) |
| 56 | } |
| 57 | return fmt.Errorf("awsjob: %s: %w: %w", op, sentinel, err) |
| 58 | } |
| 59 | |
| 60 | // tokenReusedText is the message RunMicrovm gives for a client token reused |
| 61 | // with different request parameters ("The provided clientToken was used |
| 62 | // with different request parameters.", Task 17 attempt 4). |
| 63 | const tokenReusedText = "clientToken was used with different request parameters" |
| 64 | |
| 65 | func clientTokenReused(v *types.ValidationException) bool { |
| 66 | return strings.Contains(v.ErrorMessage(), tokenReusedText) |
| 67 | } |
| 68 | |