Skip to content

Instantly share code, notes, and snippets.

@takezoux2
Last active March 20, 2022 06:12
Show Gist options
  • Save takezoux2/9a7dcef0e51294c7462e37275200bae7 to your computer and use it in GitHub Desktop.
Save takezoux2/9a7dcef0e51294c7462e37275200bae7 to your computer and use it in GitHub Desktop.
ScalaMatsuri2022 第2回ひどいコード選手権
/**
* 通知設定を取得し、ユーザーに通知を送るかどうかを判断するプログラム
*/
object Main {
def main(args: Array[String]): Unit = {
// フラグ立ってるのが通知してOKなのかどうかがもはやわからない
getConfig("User1") match {
case Some(config) if config.flag => {
// do nothing
}
case _ => {
notifyToUser("User1", "Message")
}
}
}
def getConfig(userId: String) = {
// Read from db
Some(NotificationConfig(userId, "OnReceivePrivateMessage", false))
}
def notifyToUser(userId: String, message: String) = {
// Send notification to user
println(s"Send email to ${userId}")
}
}
case class NotificationConfig(userId: String, notificationType: String, flag: Boolean)
case class Success(message: String)
case class UnknownError(message: String)
/*
実際はレイヤードアキテクチャーにより、何段か深い部分で例外が握りつぶされており、かつ同じように握りつぶしている場所が多数なので、
例外発生箇所の特定が不可能になってます。
*/
object Main {
def main(args: Array[String]): Unit = {
// 例外起きてもどこで起きているか、何が起きているかが全くわからない
val result = doAnyAction()
println(result)
}
def doAnyAction(): Success | UnknownError = {
try {
val r = coreProcess()
Success(r)
} catch {
// 握り潰した上でエラーを変更している
// ログも出ていない
case err => UnknownError("Unknown error occured")
}
}
def coreProcess = () => {
throw new Exception("Error on Database")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment