Skip to content

Commit

Permalink
fix: Fix auto-translation errors, drop some handled exceptions (sentry)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Dec 21, 2023
1 parent 6005b8c commit 1f62f24
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.tolgee.component

import io.sentry.Hint
import io.sentry.SentryEvent
import io.sentry.SentryOptions
import org.springframework.stereotype.Component

@Component
class SentryBeforeSendCallback : SentryOptions.BeforeSendCallback {
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? {
if (event.containsMessage("Failed to send message to MessageChannel")) {
return null
}

if (event.containsExceptionOfType("FailedDontRequeueException")) {
return null
}

if (event.containsExceptionOfType("ClientAbortException")) {
return null
}

if (event.containsMessage("Cannot render error page for request [/websocket")) return null

return event
}

private fun SentryEvent.containsMessage(string: String): Boolean {
return message?.formatted?.contains(string) == true
}

private fun SentryEvent.containsExceptionOfType(type: String) =
exceptions?.any { it.type?.contains(type) == true } == true
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class ExceptionWithMessage(
this.tolgeeMessage = message
}

constructor(message: Message) : this(null, null) {
constructor(message: Message) : this(message.code, null) {
this.tolgeeMessage = message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import io.tolgee.repository.AutoTranslationConfigRepository
import io.tolgee.security.authentication.AuthenticationFacade
import io.tolgee.service.LanguageService
import io.tolgee.service.machineTranslation.MtService
import io.tolgee.util.executeInNewTransaction
import io.tolgee.util.tryUntilItDoesntBreakConstraint
import jakarta.persistence.EntityManager
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.PlatformTransactionManager

@Service
class AutoTranslationService(
Expand All @@ -30,7 +33,8 @@ class AutoTranslationService(
private val languageService: LanguageService,
private val batchJobService: BatchJobService,
private val authenticationFacade: AuthenticationFacade,
private val entityManager: EntityManager
private val entityManager: EntityManager,
private val transactionManager: PlatformTransactionManager
) {
val logger: Logger = LoggerFactory.getLogger(this::class.java)

Expand Down Expand Up @@ -134,27 +138,31 @@ class AutoTranslationService(
useMachineTranslation: Boolean? = null,
isBatch: Boolean,
) {
val adjustedConfigs = getAdjustedConfigs(key, forcedLanguageTags, useTranslationMemory, useMachineTranslation)
tryUntilItDoesntBreakConstraint(maxRepeats = 10) {
executeInNewTransaction(transactionManager) {
val adjustedConfigs = getAdjustedConfigs(key, forcedLanguageTags, useTranslationMemory, useMachineTranslation)

val translations = adjustedConfigs.map {
if (it.override) {
return@map it to translationService.getOrCreate(key, it.language)
}
val translations = adjustedConfigs.map {
if (it.override) {
return@map it to translationService.getOrCreate(key, it.language)
}

it to getUntranslatedTranslations(key, listOf(it.language)).firstOrNull()
}.filter {
it.second?.state != TranslationState.DISABLED
}
it to getUntranslatedTranslations(key, listOf(it.language)).firstOrNull()
}.filter {
it.second?.state != TranslationState.DISABLED
}

val toTmTranslate = translations.filter { it.first.usingTm }.mapNotNull { it.second }
val toTmTranslate = translations.filter { it.first.usingTm }.mapNotNull { it.second }

val translatedWithTm = autoTranslateUsingTm(toTmTranslate, key).filter { it.value }.keys
val translatedWithTm = autoTranslateUsingTm(toTmTranslate, key).filter { it.value }.keys

val toMtTranslate =
translations.filter { it.first.usingPrimaryMtService && !translatedWithTm.contains(it.second) }
.mapNotNull { it.second }
val toMtTranslate =
translations.filter { it.first.usingPrimaryMtService && !translatedWithTm.contains(it.second) }
.mapNotNull { it.second }

autoTranslateUsingMachineTranslation(toMtTranslate, key, isBatch)
autoTranslateUsingMachineTranslation(toMtTranslate, key, isBatch)
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import jakarta.persistence.PersistenceException
import org.springframework.dao.CannotAcquireLockException
import org.springframework.dao.DataIntegrityViolationException

inline fun <T> tryUntilItDoesntBreakConstraint(fn: () -> T): T {
inline fun <T> tryUntilItDoesntBreakConstraint(maxRepeats: Int = 100, fn: () -> T): T {
var exception: Exception? = null
var repeats = 0
for (it in 1..100) {
for (it in 1..maxRepeats) {
try {
return fn()
} catch (e: Exception) {
Expand All @@ -16,6 +16,7 @@ inline fun <T> tryUntilItDoesntBreakConstraint(fn: () -> T): T {
repeats++
exception = e
}

else -> throw e
}
}
Expand Down

0 comments on commit 1f62f24

Please sign in to comment.