Skip to content

Conversation

@zainab-ali
Copy link
Contributor

@zainab-ali zainab-ali commented Oct 2, 2025

#192 made some classes private, and hasn't been released. I tested it out in a few more codebases, and have found some new use cases for having public exception types.

Pattern matching

While users shouldn't construct exceptions explicitly, they may want to pattern match on them:

match {
  case _: IgnoredException => ...
  case _: ExpectationFailed => ...
  case other => ...
}

Control flow with Either

Users might also want to explicitly construct ExpectationFailed for control flow. For example, the following code can't be written using our public API:

sealed trait Pet
case class Cat(name: String) extends Pet
case class Dog(name: String) extends Pet

def dogOrFailure(pet: Pet): Either[ExpectationFailed, Dog] = pet match {
  case dog: Dog => Right(dog)
  case _ => Left(new ExpectationFailed("not a dog", NonEmptyList.of(implicitly[SourceLocation]))
}

I don't think we should expose the ExpectationFailed constructor in the long term, but can do so in the short term and replace it with a helper function in future.

@zainab-ali zainab-ali force-pushed the relax-private-exception-constraint branch 2 times, most recently from fbb7f40 to ffca1b7 Compare October 2, 2025 12:43
@zainab-ali zainab-ali force-pushed the relax-private-exception-constraint branch from ffca1b7 to 736c80d Compare October 2, 2025 13:16
@zainab-ali zainab-ali marked this pull request as ready for review October 2, 2025 14:45
@zainab-ali zainab-ali merged commit 2be2731 into typelevel:main Oct 2, 2025
13 checks passed
@zainab-ali zainab-ali deleted the relax-private-exception-constraint branch October 15, 2025 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants