Skip to content

Commit

Permalink
refactor: ExceptionTranslator 구현 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Jul 28, 2024
1 parent 05ff435 commit 0b8044f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package com.few.api.repo.common

import org.jooq.ExecuteContext
import org.jooq.ExecuteListener
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
import org.springframework.jdbc.support.SQLExceptionTranslator

class ExceptionTranslator : ExecuteListener {
class ExceptionTranslator(
private val translator: SQLExceptionTranslator,
) : ExecuteListener {

override fun exception(context: ExecuteContext) {
val dialect = context.configuration().dialect()
val translator: SQLExceptionTranslator = SQLErrorCodeSQLExceptionTranslator(dialect.name)
context.exception(
translator
.translate("Access database using Jooq", context.sql(), context.sqlException()!!)
Expand Down
12 changes: 4 additions & 8 deletions api-repo/src/main/kotlin/com/few/api/repo/config/JooqConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import org.jooq.SQLDialect
import org.jooq.impl.DataSourceConnectionProvider
import org.jooq.impl.DefaultConfiguration
import org.jooq.impl.DefaultDSLContext
import org.jooq.impl.DefaultExecuteListenerProvider
import org.springframework.context.ApplicationEventPublisher
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
import javax.sql.DataSource

@Configuration
Expand All @@ -27,8 +27,9 @@ class JooqConfig(
fun configuration(): DefaultConfiguration {
val jooqConfiguration = DefaultConfiguration()
jooqConfiguration.set(connectionProvider())
jooqConfiguration.set(DefaultExecuteListenerProvider(exceptionTransformer()))
jooqConfiguration.set(NativeSQLLogger(), PerformanceListener(applicationEventPublisher))
val translator =
SQLErrorCodeSQLExceptionTranslator(SQLDialect.MYSQL.name)
jooqConfiguration.set(ExceptionTranslator(translator), NativeSQLLogger(), PerformanceListener(applicationEventPublisher))
jooqConfiguration.set(SQLDialect.MYSQL)
return jooqConfiguration
}
Expand All @@ -37,9 +38,4 @@ class JooqConfig(
fun connectionProvider(): DataSourceConnectionProvider {
return DataSourceConnectionProvider(dataSource)
}

@Bean
fun exceptionTransformer(): ExceptionTranslator {
return ExceptionTranslator()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import jooq.jooq_dsl.tables.Member
import org.jooq.DSLContext
import org.jooq.JSON
import org.jooq.exception.DataAccessException
import org.jooq.exception.IntegrityConstraintViolationException
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -58,7 +57,7 @@ class _SampleJooqTest : JooqTestSpec() {
@Transactional
fun `이메일이 중복되는 경우 저장에 실패합니다`() {
// when & then
assertThrows<IntegrityConstraintViolationException> {
assertThrows<DataAccessException> {
dslContext.insertInto(Member.MEMBER)
.set(Member.MEMBER.EMAIL, EMAIL)
.set(Member.MEMBER.TYPE_CD, TYPECD)
Expand Down

0 comments on commit 0b8044f

Please sign in to comment.