Skip to content

feat: error translator support #162

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

Merged
merged 4 commits into from
Mar 9, 2023

Conversation

saeidee
Copy link
Member

@saeidee saeidee commented Feb 21, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This PR adds support for translating Postgres errors like unique key violation to native gorm errors (ErrDuplicatedKey)

Related PR in gorm repo: go-gorm/gorm#6004

Sorry, something went wrong.

}

func (dialector Dialector) Translate(err error) error {
parsedErr, marshalErr := json.Marshal(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to assert the error type for performance? e.g:

pgErr, ok := err.(*pgconn.PgError)

And use JSON marshal/unmarshal only when failing to assert

Copy link
Member Author

@saeidee saeidee Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe since it is driver related native error, only checking the PgError is enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but it might be broken if using a different pg driver... but anyway, both are fine for me.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

		var p *pgconn.PgError
		if errors.As(err, &p) && p.Code == "23505" {
			if strings.Contains(p.Detail, "email") {
				return nil, errno.DBError(errno.ErrEmailExist, "注册商户失败")
			}
			if strings.Contains(p.Detail, "phone") {
				return nil, errno.DBError(errno.ErrMerchantPhoneExist, "注册商户失败")
			}
		}

How should I tell which key is conflicting in the new version ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I think this has been breaking API, The original code cannot be reached after updating the new version,Resulting in users not knowing whether the mailbox is duplicated or the phone number is duplicated,So I use v1.4.8

Copy link
Member Author

@saeidee saeidee Mar 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can enhance the error details, @jinzhu what do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 @Naist4869. Previously, I could parse pgConn.pgError for the constraint name and handle each constraint violation differently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agree this is a breaking API, the good news we still haven't released a new version for GORM, so these changes won't affected users unless they are using the latest master.

Maybe we need to add a configuration TranslateError to GORM Config, https://github.com/go-gorm/gorm/blob/master/gorm.go#L20, by default we don't translate the error.

@saeidee do you feel good to make the change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can implement the changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the PR go-gorm/gorm#6178

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants