@@ -36,11 +36,11 @@ type DynamoDBClient interface {
3636 GetItem (ctx context.Context , params * dynamodb.GetItemInput , optFns ... func (* dynamodb.Options )) (* dynamodb.GetItemOutput , error )
3737}
3838
39- func isTableNotFoundExceptionError (err error ) bool {
39+ func isResourceNotFoundException (err error ) bool {
4040 var apiError smithy.APIError
4141 if errors .As (err , & apiError ) {
4242 switch apiError .(type ) {
43- case * types.TableNotFoundException :
43+ case * types.ResourceNotFoundException :
4444 return true
4545 }
4646 }
@@ -55,7 +55,7 @@ func (dynamoDbLock *DynamoDbLock) waitUntilTableCreated(ctx context.Context) err
5555 cnt := 0
5656
5757 if err != nil {
58- if ! isTableNotFoundExceptionError (err ) {
58+ if ! isResourceNotFoundException (err ) {
5959 return err
6060 }
6161 }
@@ -69,23 +69,23 @@ func (dynamoDbLock *DynamoDbLock) waitUntilTableCreated(ctx context.Context) err
6969 time .Sleep (TableCreationInterval )
7070 status , err = dynamoDbLock .DynamoDb .DescribeTable (ctx , input )
7171 if err != nil {
72- if ! isTableNotFoundExceptionError (err ) {
72+ if ! isResourceNotFoundException (err ) {
7373 return err
7474 }
7575 }
7676 cnt ++
7777 if cnt > TableCreationRetryCount {
78- slog .Error ("DynamoDB table creation timed out" ,
78+ msg := "DynamoDB table creation timed out"
79+ slog .Error (msg ,
7980 "tableName" , TABLE_NAME ,
8081 "retryCount" , cnt )
81- os . Exit ( 1 )
82+ return errors . New ( msg )
8283 }
8384 }
8485
8586 return nil
8687}
8788
88- // TODO: refactor func to return actual error and fail on callers
8989func (dynamoDbLock * DynamoDbLock ) createTableIfNotExists (ctx context.Context ) error {
9090 _ , err := dynamoDbLock .DynamoDb .DescribeTable (ctx , & dynamodb.DescribeTableInput {
9191 TableName : aws .String (TABLE_NAME ),
@@ -94,7 +94,7 @@ func (dynamoDbLock *DynamoDbLock) createTableIfNotExists(ctx context.Context) er
9494 slog .Debug ("DynamoDB table already exists" , "tableName" , TABLE_NAME )
9595 return nil
9696 }
97- if ! isTableNotFoundExceptionError (err ) {
97+ if ! isResourceNotFoundException (err ) {
9898 slog .Info ("Error describing DynamoDB table, proceeding to create" , "tableName" , TABLE_NAME , "error" , err )
9999 }
100100
@@ -148,7 +148,12 @@ func (dynamoDbLock *DynamoDbLock) Lock(transactionId int, resource string) (bool
148148 "resource" , resource ,
149149 "transactionId" , transactionId )
150150
151- dynamoDbLock .createTableIfNotExists (ctx )
151+ err := dynamoDbLock .createTableIfNotExists (ctx )
152+ if err != nil {
153+ slog .Error ("Error creating DynamoDB table" , "error" , err )
154+ return false , err
155+ }
156+
152157 // TODO: remove timeout completely
153158 now := time .Now ().Format (time .RFC3339 )
154159 newTimeout := time .Now ().Add (TableLockTimeout ).Format (time .RFC3339 )
@@ -211,7 +216,12 @@ func (dynamoDbLock *DynamoDbLock) Unlock(resource string) (bool, error) {
211216
212217 slog .Debug ("Attempting to release lock" , "resource" , resource )
213218
214- dynamoDbLock .createTableIfNotExists (ctx )
219+ err := dynamoDbLock .createTableIfNotExists (ctx )
220+ if err != nil {
221+ slog .Error ("Error creating DynamoDB table" , "error" , err )
222+ return false , err
223+ }
224+
215225 input := & dynamodb.DeleteItemInput {
216226 TableName : aws .String (TABLE_NAME ),
217227 Key : map [string ]types.AttributeValue {
@@ -220,7 +230,7 @@ func (dynamoDbLock *DynamoDbLock) Unlock(resource string) (bool, error) {
220230 },
221231 }
222232
223- _ , err : = dynamoDbLock .DynamoDb .DeleteItem (ctx , input )
233+ _ , err = dynamoDbLock .DynamoDb .DeleteItem (ctx , input )
224234 if err != nil {
225235 slog .Error ("Failed to delete DynamoDB item for lock" , "resource" , resource , "error" , err )
226236 return false , err
@@ -235,7 +245,12 @@ func (dynamoDbLock *DynamoDbLock) GetLock(lockId string) (*int, error) {
235245
236246 slog .Debug ("Getting lock information" , "lockId" , lockId )
237247
238- dynamoDbLock .createTableIfNotExists (ctx )
248+ err := dynamoDbLock .createTableIfNotExists (ctx )
249+ if err != nil {
250+ slog .Error ("Error creating DynamoDB table" , "error" , err )
251+ return nil , err
252+ }
253+
239254 input := & dynamodb.GetItemInput {
240255 TableName : aws .String (TABLE_NAME ),
241256 Key : map [string ]types.AttributeValue {
0 commit comments