Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce common enum/format column exception #102

Closed
stewSquared opened this issue Jan 10, 2018 · 1 comment
Closed

Introduce common enum/format column exception #102

stewSquared opened this issue Jan 10, 2018 · 1 comment

Comments

@stewSquared
Copy link
Contributor

Often, a failure when parsing a string from a database column indicates not an expected bad input, but some sort of inconsistent or illegal state in the database, since data is validated on the way in (by convention). This happens so often over all our EntityRowConversion traits, that independent helper methods .

In referrals:

  private def expectValid[ADT](fromString: String => Option[ADT], column: String)(
      implicit adtClass: ClassTag[ADT]): ADT = {
    fromString(column).getOrElse(throw new DatabaseException(s"corrupted value found for $adtClass: $column"))
  }

In payments:

  def useFromStringToConvertOrDie[T](converter: { def fromString(s: String): Option[T] }, fieldValue: String): T =
    converter.fromString(fieldValue).getOrElse(throw new IllegalStateException("Unrecognized field value."))

In advocates:

  def entityFromString[T](entityStr: String, mapper: (String => Option[T]), entityName: String): T =
    mapper(entityStr).getOrElse(throw DatabaseException(s"Invalid $entityName in database: $entityStr"))

Elsewhere, ad-hoc Exceptions (not necessarly ServiceException) are thrown, and in far too many places, we skip error reporting altogether and fill in fake data, or otherwise hide the distinction between error and optional states:

  email.flatMap(Email.parse).getOrElse(Email("not", "set"))`

Let's put something into xyz.driver.core.database and standardize. I recommend the first snippet with possible renaming.

See also the more specific case of UUIDs: #82

@stewSquared
Copy link
Contributor Author

Resolved by #109

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

No branches or pull requests

2 participants