From 90cb42b367d220b140fcb8e760e7b0704bf56861 Mon Sep 17 00:00:00 2001 From: Saeid Saeidee Date: Fri, 19 May 2023 15:59:30 +0200 Subject: [PATCH] Revert "added 23503 pgerrcode support and updated the error conversion approach (#185)" This reverts commit b320a5cdf7d6838a37bc0272f619dfbc031fc23e. --- error_translator.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 }