Skip to content

Commit

Permalink
Better error messages when can't map because a class has more than 22…
Browse files Browse the repository at this point in the history
… fields
  • Loading branch information
adamw committed Jan 11, 2024
1 parent e28308f commit 9cd1800
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 12 additions & 5 deletions core/src/main/scala-2/sttp/tapir/internal/MapToMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,21 @@ private[tapir] object MapToMacro {
): Unit = {
val tupleSymbol = tupleType.typeSymbol
if (!tupleSymbol.fullName.startsWith("scala.Tuple") && caseClassUtil.fields.nonEmpty) {
c.abort(c.enclosingPosition, s"Expected source type to be a tuple, but got: $tupleType")
c.abort(c.enclosingPosition, s"Expected source type to be a tuple, but got: ${tupleType.dealias}")
}

if (caseClassUtil.fields.size != tupleTypeArgs.size) {
c.abort(
c.enclosingPosition,
s"The arity of the source type doesn't match the arity of the target type: $tupleType, ${caseClassUtil.t}"
)
if (caseClassUtil.fields.size > 22) {
c.abort(
c.enclosingPosition,
s"Cannot map to ${caseClassUtil.t}: arities of up to 22 are supported. If you need more inputs/outputs, map them to classes with less fields, and then combine these classes."
)
} else {
c.abort(
c.enclosingPosition,
s"The arity of the source type (${tupleTypeArgs.size}) doesn't match the arity of the target type (${caseClassUtil.fields.size}): ${tupleType.dealias}, ${caseClassUtil.t}"
)
}
}

caseClassUtil.fields.zip(tupleTypeArgs).foreach { case (caseClassField, tupleArg) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ private[tapir] object ComplietimeErrors {
private def reportIncorrectMappingImpl[SOURCE: Type, TARGET: Type](using Quotes): Expr[Unit] = {
import quotes.reflect.*

report.throwError(s"Failed to map ${Type.show[SOURCE]} into ${Type.show[TARGET]}")
if TypeRepr.of[TARGET].typeSymbol.declaredFields.size > 22 then
report.throwError(
s"Cannot map to ${Type.show[TARGET]}: arities of up to 22 are supported. If you need more inputs/outputs, map them to classes with less fields, and then combine these classes."
)
else report.throwError(s"Failed to map ${Type.show[SOURCE]} into ${Type.show[TARGET]}")
}

}

0 comments on commit 9cd1800

Please sign in to comment.