|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + openapi "github.com/brevdev/cloud/internal/lambdalabs/gen/lambdalabs" |
| 11 | + v1 "github.com/brevdev/cloud/pkg/v1" |
| 12 | + "github.com/cenkalti/backoff/v4" |
| 13 | +) |
| 14 | + |
| 15 | +func handleAPIError(ctx context.Context, resp *http.Response, err error) error { |
| 16 | + body := "" |
| 17 | + e, ok := err.(openapi.GenericOpenAPIError) |
| 18 | + if ok { |
| 19 | + body = string(e.Body()) |
| 20 | + } |
| 21 | + if body == "" { |
| 22 | + bodyBytes, errr := io.ReadAll(resp.Body) |
| 23 | + if errr != nil { |
| 24 | + fmt.Printf("Error reading response body: %v\n", errr) |
| 25 | + } |
| 26 | + body = string(bodyBytes) |
| 27 | + } |
| 28 | + outErr := fmt.Errorf("LambdaLabs API error\n%s\n%s:\nErr: %s\n%s", resp.Request.URL, resp.Status, err.Error(), body) |
| 29 | + if strings.Contains(body, "instance does not exist") { //nolint:gocritic // ignore |
| 30 | + return backoff.Permanent(v1.ErrInstanceNotFound) |
| 31 | + } else if strings.Contains(body, "banned you temporarily") { |
| 32 | + return outErr |
| 33 | + } else if resp.StatusCode < 500 && resp.StatusCode != 429 { // 429 Too Many Requests (use back off) |
| 34 | + return backoff.Permanent(outErr) |
| 35 | + } else { |
| 36 | + return outErr |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func handleErrToCloudErr(e error) error { |
| 41 | + if e == nil { |
| 42 | + return nil |
| 43 | + } |
| 44 | + if strings.Contains(e.Error(), "Not enough capacity") || strings.Contains(e.Error(), "insufficient-capacity") { //nolint:gocritic // ignore |
| 45 | + return v1.ErrInsufficientResources |
| 46 | + } else if strings.Contains(e.Error(), "global/invalid-parameters") && strings.Contains(e.Error(), "Region") && strings.Contains(e.Error(), "does not exist") { |
| 47 | + return v1.ErrInsufficientResources |
| 48 | + } else { |
| 49 | + return e |
| 50 | + } |
| 51 | +} |
0 commit comments