AWScala icon indicating copy to clipboard operation
AWScala copied to clipboard

Option[Table] results in NoSuchElementException

Open mjuchli opened this issue 10 years ago • 4 comments

Hi there,

Checking if a table exists :

dynamoDB.table("mytable").nonEmpty

I expect to get either None or Table. Instead, it results in:

java.util.NoSuchElementException: None.get
    at scala.None$.get(Option.scala:347)
    at scala.None$.get(Option.scala:345)
    at awscala.dynamodbv2.TableMeta.table(TableMeta.scala:36)
    at awscala.dynamodbv2.DynamoDB$$anonfun$table$1.apply(DynamoDB.scala:53)
    at awscala.dynamodbv2.DynamoDB$$anonfun$table$1.apply(DynamoDB.scala:53)
    at scala.Option.map(Option.scala:146)
    at awscala.dynamodbv2.DynamoDB$class.table(DynamoDB.scala:53)
    at awscala.dynamodbv2.DynamoDBClient.table(DynamoDB.scala:338)

Reason for this is that table function will call get on an option type:

hashPK = keySchema.find(_.keyType == aws.model.KeyType.HASH).get.attributeName,

while the keySchema may not have a hash key, for example when a table doesn't exist at all.

mjuchli avatar Oct 28 '15 10:10 mjuchli

I think these operations should return a disjunction @seratch would you be willing to have scalaz./ on these types?

larroy avatar Mar 10 '16 12:03 larroy

+1 here

Similar issue:

java.util.NoSuchElementException: None.get
        at scala.None$.get(Option.scala:313)
        at scala.None$.get(Option.scala:311)
        at awscala.dynamodbv2.DynamoDB$class.putItem(DynamoDB.scala:208)
        at awscala.dynamodbv2.DynamoDBClient.putItem(DynamoDB.scala:348)
        at awscala.dynamodbv2.DynamoDB$class.put(DynamoDB.scala:205)
        at awscala.dynamodbv2.DynamoDBClient.put(DynamoDB.scala:348)
        at awscala.dynamodbv2.Table.putItem(Table.scala:42)
        at awscala.dynamodbv2.Table.put(Table.scala:36)

This is triggered by a simple dynamoDB.table(TableName).get.put(...).

Edit: I was supplying a range key and my table was misconfigured. Still, an opaque error message.

findchris avatar Jun 02 '16 22:06 findchris

@findchris In your case it makes sense to get NSE because you call .get on the optional value which fails if it's None, I'm just wondering why it happens in case of dynamoDB.table("mytable").nonEmpty because nonEmpty obviously just delegates the job to isDefined method for an Option which is a safe operation!

@backender are you sure you get the exception exactly on that line of the code? I never got such an exception using DynamoDB with AWScala recently.

naderghanbari avatar Oct 18 '16 08:10 naderghanbari

@naderghanbari I'm not actively using the library anymore but since I remember spending quiet some time with the issue, I presume what I documented was what I experienced.

mjuchli avatar Oct 18 '16 08:10 mjuchli