Skip to content

Commit f880b61

Browse files
authored
Return an explicit error if getShardId is out of bounds (#160)
* Return an explicit error if getShardId is out of bounds * Imports * Include entity id in error * >= 1
1 parent 8d6b223 commit f880b61

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ package-lock.json
2222
node_modules/
2323
.DS_Store
2424
vuepress/docs/.vuepress/dist
25+
project/project/metals.sbt
26+
project/project/project/metals.sbt

entities/src/main/scala/com/devsisters/shardcake/Sharding.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.devsisters.shardcake
22

33
import com.devsisters.shardcake.Messenger.MessengerTimeout
44
import com.devsisters.shardcake.Sharding.{ EntityState, ShardingRegistrationEvent }
5-
import com.devsisters.shardcake.errors.{ EntityNotManagedByThisPod, PodUnavailable, SendTimeoutException }
5+
import com.devsisters.shardcake.errors._
66
import com.devsisters.shardcake.interfaces.Pods.BinaryMessage
77
import com.devsisters.shardcake.interfaces.{ Pods, Serialization, Storage }
88
import com.devsisters.shardcake.internal.{ EntityManager, ReplyChannel, SendChannel }
@@ -406,7 +406,8 @@ class Sharding private (
406406
}
407407
} yield ()
408408

409-
trySend
409+
if (shardId >= 1 && shardId <= config.numberOfShards) trySend
410+
else ZIO.fail(InvalidShardId(entityId, shardId))
410411
}
411412

412413
private def sendStreamGeneric[Res](
@@ -438,7 +439,8 @@ class Sharding private (
438439
}
439440
} yield ()
440441

441-
trySend
442+
if (shardId >= 1 && shardId <= config.numberOfShards) trySend
443+
else ZIO.fail(InvalidShardId(entityId, shardId))
442444
}
443445
}
444446

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.devsisters.shardcake.errors
2+
3+
/**
4+
* Exception indicating that a shard id is invalid.
5+
*/
6+
case class InvalidShardId(entityId: String, shardId: Int)
7+
extends Exception(s"Invalid shard id: $shardId for entity $entityId")

0 commit comments

Comments
 (0)