Skip to content

Commit

Permalink
Use only DeserializationError as an error in deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Jan 9, 2024
1 parent 7be0624 commit 53ce389
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions oolong-bson/src/main/scala/oolong/bson/BsonDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import scala.util.Try
import magnolia1.*
import oolong.bson.annotation.*
import oolong.bson.meta.QueryMeta
import org.bson.BsonInvalidOperationException
import org.bson.BsonNull
import org.mongodb.scala.bson.*

Expand Down Expand Up @@ -58,7 +57,7 @@ object BsonDecoder {
Try(
pf.applyOrElse[BsonValue, T](
value,
bv => throw new BsonInvalidOperationException(s"Can't decode $bv")
bv => throw DeserializationError(s"Can't decode $bv")
)
)

Expand Down Expand Up @@ -104,7 +103,7 @@ object BsonDecoder {
.partitionMap { case (instance, name) =>
for {
value <- Option(value.asDocument.getFieldOpt(${ Expr(map) }.getOrElse(name, name)).getOrElse(BsonNull()))
.toRight(new RuntimeException(s"Not found value $name"))
.toRight(DeserializationError(s"Not found value $name"))
defaults = magnolia1.Macro.defaultValue[T]
result <- instance.asInstanceOf[BsonDecoder[Any]].fromBson(value).toEither match {
case Left(exc) =>
Expand Down Expand Up @@ -142,14 +141,14 @@ object BsonDecoder {
val result = for {
descriminatorFromBsonValue <- Option(value.asDocument.get(discriminatorField))
.map(_.asString)
.toRight(new RuntimeException(s"Not found discriminator field $discriminatorField"))
.toRight(DeserializationError(s"Not found discriminator field $discriminatorField"))
.map(_.getValue)
index <- ${ Expr(names) }
.collectFirst {
case (className, id) if descriminatorFromBsonValue == modifyValue(className) => id
}
.toRight(
new RuntimeException(
DeserializationError(
s"$descriminatorFromBsonValue does not match any of ${${ Expr(names) }.map(v => modifyValue(v._1)).mkString("[", ", ", "]")}"
)
)
Expand Down

0 comments on commit 53ce389

Please sign in to comment.