fix(service): log translation errors and hide internals from clients

Return a generic "Translation failed" message instead of echoing
err.Error() in the HTTP response, and log the underlying error
server-side via log.Printf. Avoids leaking internal details (proxy
address, upstream URL, etc.) to API clients while keeping the 500
behavior introduced in #224.
This commit is contained in:
Vincent Young 2026-06-02 14:38:02 +08:00
parent d19a3902df
commit 9d6b294a61
No known key found for this signature in database
GPG Key ID: 070D9CD629BC1AAE

View File

@ -133,9 +133,10 @@ func Router(cfg *Config) *gin.Engine {
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, "") result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, "")
if err != nil { if err != nil {
log.Printf("Translation failed: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError, "code": http.StatusInternalServerError,
"message": "Translation failed: " + err.Error(), "message": "Translation failed",
}) })
return return
} }
@ -207,9 +208,10 @@ func Router(cfg *Config) *gin.Engine {
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, dlSession) result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, dlSession)
if err != nil { if err != nil {
log.Printf("Translation failed: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError, "code": http.StatusInternalServerError,
"message": "Translation failed: " + err.Error(), "message": "Translation failed",
}) })
return return
} }
@ -263,9 +265,10 @@ func Router(cfg *Config) *gin.Engine {
result, err := translate.TranslateByDeepLX("", targetLang, translateText, "", proxyURL, "") result, err := translate.TranslateByDeepLX("", targetLang, translateText, "", proxyURL, "")
if err != nil { if err != nil {
log.Printf("Translation failed: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError, "code": http.StatusInternalServerError,
"message": "Translation failed: " + err.Error(), "message": "Translation failed",
}) })
return return
} }