fix(service): return 500 instead of crashing on translation error (#224)

The /translate, /v1/translate and /v2/translate handlers called log.Fatalf when TranslateByDeepLX returned an error. log.Fatalf calls os.Exit, so a single failed translation would tear down the whole server process. Return a 500 JSON response and keep serving instead.
This commit is contained in:
Bonny Lin 2026-05-31 19:47:09 +09:00 committed by GitHub
parent 1b88e428ae
commit 3f099d187c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -127,7 +127,11 @@ func Router(cfg *Config) *gin.Engine {
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, "")
if err != nil {
log.Fatalf("Translation failed: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": "Translation failed: " + err.Error(),
})
return
}
if result.Code == http.StatusOK {
@ -191,7 +195,11 @@ func Router(cfg *Config) *gin.Engine {
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, dlSession)
if err != nil {
log.Fatalf("Translation failed: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": "Translation failed: " + err.Error(),
})
return
}
if result.Code == http.StatusOK {
@ -243,7 +251,11 @@ func Router(cfg *Config) *gin.Engine {
result, err := translate.TranslateByDeepLX("", targetLang, translateText, "", proxyURL, "")
if err != nil {
log.Fatalf("Translation failed: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": "Translation failed: " + err.Error(),
})
return
}
if result.Code == http.StatusOK {