callWithClient is a helper function to call S3 client method with automatic region error handling
(s *Storage, bucket string, fn func(client s3Client) (*T, error))
| 198 | // callWithClient is a helper function to call S3 client method with automatic region |
| 199 | // error handling |
| 200 | func callWithClient[T any](s *Storage, bucket string, fn func(client s3Client) (*T, error)) (*T, s3Client, error) { |
| 201 | client := s.getBucketClient(bucket) |
| 202 | |
| 203 | r, err := fn(client) |
| 204 | |
| 205 | if err != nil { |
| 206 | // Check if the error is the region mismatch error. |
| 207 | // If so, create a new client with the correct region and retry the request. |
| 208 | if region := regionFromError(err); len(region) != 0 { |
| 209 | client, err = s.createBucketClient(bucket, region) |
| 210 | if err != nil { |
| 211 | return nil, nil, err |
| 212 | } |
| 213 | |
| 214 | r, err = fn(client) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if err != nil { |
| 219 | return nil, nil, err |
| 220 | } |
| 221 | |
| 222 | return r, client, nil |
| 223 | } |
no test coverage detected