Throwing methods are uncommon in scala, let's throw at scala/java boundary
diff --git a/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala b/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala
index c02c1aa..9eda3cc 100644
--- a/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala
+++ b/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala
@@ -37,16 +37,8 @@
type AllowedQuantity = Long Refined PositiveLongConstraint
def validate(value: Long): Either[NumberFormatException, AllowedQuantity] =
- refined.refineV[PositiveLongConstraint](value) match {
- case Right(value) => Right(value)
- case Left(error) => Left(new NumberFormatException(error))
- }
-
- def liftOrThrow(value: Long): AllowedQuantity =
- validate(value) match {
- case Right(value) => value
- case Left(error) => throw error
- }
+ refined.refineV[PositiveLongConstraint](value)
+ .left.map(error => new NumberFormatException(error))
}
object Increment {
@@ -54,16 +46,8 @@
type Increment = Int Refined PositiveLongConstraint
def validate(value: Int): Either[NumberFormatException, Increment] =
- refined.refineV[PositiveLongConstraint](value) match {
- case Right(value) => Right(value)
- case Left(error) => Left(new NumberFormatException(error))
- }
-
- def liftOrThrow(value: Int): Increment =
- validate(value) match {
- case Right(value) => value
- case Left(error) => throw error
- }
+ refined.refineV[PositiveLongConstraint](value)
+ .left.map(error => new NumberFormatException(error))
}
case class Rule(quantity: AllowedQuantity, duration: Duration)
diff --git a/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala b/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala
index ead8e82..a93bfb5 100644
--- a/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala
+++ b/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala
@@ -56,21 +56,32 @@
}
object EntityType {
+
+ implicit class EitherOps[E <: Throwable, A](either: Either[E, A]) {
+ def orThrow(message: String): A = either.left.map(cause => new IllegalArgumentException(message, cause)).toTry.get
+ }
+
def extractRules(entityType: EntityType, duration: Duration, mailetConfig: MailetConfig): Option[Rules] = (entityType match {
case Count => mailetConfig.getOptionalLong("count")
case RecipientsType => mailetConfig.getOptionalLong("recipients")
case Size => mailetConfig.getOptionalSize("size").map(_.asBytes())
case TotalSize => mailetConfig.getOptionalSize("totalSize").map(_.asBytes())
- }).map(AllowedQuantity.liftOrThrow)
+ }).map(AllowedQuantity.validate(_).orThrow(s"invalid quantity for ${entityType.asString}"))
.map(quantity => Rules(Seq(Rule(quantity, duration))))
def extractQuantity(entityType: EntityType, mail: Mail): Option[Increment] = entityType match {
case Count => Some(1)
- case RecipientsType => Some(Increment.liftOrThrow(mail.getRecipients.size()))
- case Size => Some(Increment.liftOrThrow(mail.getMessageSize.toInt))
+ case RecipientsType =>
+ Some(Increment
+ .validate(mail.getRecipients.size())
+ .orThrow(s"invalid quantity for ${entityType.asString}"))
+ case Size =>
+ Some(Increment
+ .validate(mail.getMessageSize.toInt)
+ .orThrow(s"invalid quantity for ${entityType.asString}"))
case TotalSize =>
Try(Math.multiplyExact(mail.getMessageSize.toInt, mail.getRecipients.size()))
- .map(Increment.liftOrThrow)
+ .map(Increment.validate(_).orThrow(s"invalid quantity for ${entityType.asString}"))
.toOption
}
}