-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix: rollback if email already exists (#157)
- Loading branch information
1 parent
d57f176
commit 1db3c37
Showing
2 changed files
with
92 additions
and
20 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/com/hypto/iam/server/exceptions/DbExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.hypto.iam.server.exceptions | ||
|
||
import io.ktor.server.plugins.BadRequestException | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.primaryConstructor | ||
import org.jooq.exception.DataAccessException | ||
|
||
object DbExceptionHandler { | ||
data class DbExceptionMap( | ||
val errorMessage: String, | ||
val constraintRegex: Regex, | ||
val appExceptions: Map<String, Pair<KClass<out Exception>, String>> | ||
) | ||
|
||
private val customExceptions = setOf( | ||
BadRequestException::class, | ||
EntityNotFoundException::class, | ||
EntityAlreadyExistsException::class | ||
) | ||
private val duplicateConstraintRegex = "\"(.*)?\"".toRegex() | ||
private val foreignKeyConstraintRegex = "foreign key constraint \"(.*)?\"".toRegex() | ||
|
||
private val duplicateExceptions = mapOf<String, Pair<KClass<out Exception>, String>>() | ||
|
||
private val foreignKeyExceptions = mapOf<String, Pair<KClass<out Exception>, String>>() | ||
|
||
private val dbExceptionMap = listOf( | ||
DbExceptionMap( | ||
"duplicate key value violates unique constraint", | ||
duplicateConstraintRegex, | ||
duplicateExceptions | ||
), | ||
DbExceptionMap( | ||
"violates foreign key constraint", | ||
foreignKeyConstraintRegex, | ||
foreignKeyExceptions | ||
) | ||
) | ||
|
||
fun mapToApplicationException(e: DataAccessException): Exception { | ||
var finalException: Exception? = null | ||
val causeMessage = e.cause?.message ?: e.message | ||
|
||
e.cause?.let { | ||
if (customExceptions.contains(it::class)) { | ||
return it as Exception | ||
} | ||
} | ||
|
||
dbExceptionMap.forEach { | ||
if (causeMessage?.contains(it.errorMessage) == true) { | ||
val constraintKey = it.constraintRegex.find(causeMessage)?.groups?.get(1)?.value | ||
val exceptionPair = it.appExceptions[constraintKey] | ||
if (exceptionPair != null) { | ||
finalException = exceptionPair.first.primaryConstructor?.call(exceptionPair.second, e) | ||
} | ||
} | ||
} | ||
|
||
return finalException ?: InternalException("Unknown error occurred", e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters