Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow enabling TranslateError for session too instead of only globally #7253

Open
OrkhanAlikhanov opened this issue Oct 23, 2024 · 0 comments
Assignees
Labels
type:feature_request feature request

Comments

@OrkhanAlikhanov
Copy link

OrkhanAlikhanov commented Oct 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request
Projects
None yet
Development

No branches or pull requests

2 participants