diff --git a/error_translator.go b/error_translator.go index af2498a..285494c 100644 --- a/error_translator.go +++ b/error_translator.go @@ -6,9 +6,8 @@ import ( "gorm.io/gorm" ) -var errDesToGormErrs = map[string]error{ - "23505": gorm.ErrDuplicatedKey, - "23503": gorm.ErrForeignKeyViolated, +var errCodes = map[string]string{ + "uniqueConstraint": "23505", } type ErrMessage struct { @@ -21,9 +20,8 @@ type ErrMessage struct { // Since currently gorm supporting both pgx and pg drivers, only checking for pgx PgError types is not enough for translating errors, so we have additional error json marshal fallback. func (dialector Dialector) Translate(err error) error { if pgErr, ok := err.(*pgconn.PgError); ok { - gormErr, ok := errDesToGormErrs[pgErr.Code] - if ok { - return gormErr + if pgErr.Code == errCodes["uniqueConstraint"] { + return gorm.ErrDuplicatedKey } return err }