Describe the feature
Allow enabling TranslateError
for gorm.Session
instead of only globally (gorm.Config
)
Motivation
I'd like to enable TranslateError
option for a session instead of globally. I want to be able detect duplicate key error across dialects but without enabling it for the entire app as translated error loses the useful information like why the duplicate key information was raised.
It would be nicer if gorm errors had Unwrap()
method implemented but they don't and probably won't.
Example usage:
tx := DB.Session(&gorm.Session{TranslateError: true})
err := tx.Create(...).Error
if errors.Is(err, gorm.ErrDuplicatedKey) {
// do something
}
Workaround is to modify the copied Config manually:
tx := DB.Session(&gorm.Session{TranslateError: true})
tx.Config.TranslateError = true // <-- Added this line
err := tx.Create(...).Error
if errors.Is(err, gorm.ErrDuplicatedKey) {
// do something
}
Related Issues
Didn't find related issues
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days