Skip to content

Commit

Permalink
Merge pull request #320 from fd4s/preserve-throwable
Browse files Browse the repository at this point in the history
Preserve original exception when converting to AvroError
  • Loading branch information
bplommer authored Apr 21, 2021
2 parents 6c96b28 + b8171fb commit bff01f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ lazy val mimaSettings = Seq(
ProblemFilters.exclude[IncompatibleSignatureProblem]("*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("vulcan.Codec.withDecodingTypeName"),
ProblemFilters.exclude[DirectMissingMethodProblem]("vulcan.AvroError.decode*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("vulcan.AvroError.encode*")
ProblemFilters.exclude[DirectMissingMethodProblem]("vulcan.AvroError.encode*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("vulcan.AvroException.*")
)
// format: on
}
Expand Down
9 changes: 7 additions & 2 deletions modules/core/src/main/scala/vulcan/AvroError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ object AvroError {
s"Symbol $symbol is not part of schema symbols [${symbols.mkString(", ")}]"
}

private[vulcan] final def fromThrowable(throwable: Throwable): AvroError =
AvroError(throwable.toString)
private[vulcan] final def fromThrowable(cause: Throwable): AvroError = {
new AvroError {
def message: String = cause.toString

def throwable: Throwable = AvroException(message, Some(cause))
}
}
}
7 changes: 4 additions & 3 deletions modules/core/src/main/scala/vulcan/AvroException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ package vulcan
* `Throwable` representation of an [[AvroError]],
* created with [[AvroError#throwable]].
*/
sealed abstract class AvroException(message: String) extends RuntimeException(message)
sealed abstract class AvroException(message: String, cause: Option[Throwable])
extends RuntimeException(message, cause.orNull)

private[vulcan] object AvroException {
final def apply(message: String): AvroException =
new AvroException(message) {
final def apply(message: String, cause: Option[Throwable] = None): AvroException =
new AvroException(message, cause) {
override final def toString: String =
s"vulcan.AvroException: $getMessage"
}
Expand Down

0 comments on commit bff01f0

Please sign in to comment.