26 lines
562 B
Go
26 lines
562 B
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// IsDuplicateKeyError checks if an error is a duplicate key constraint violation
|
|
func IsDuplicateKeyError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
|
|
// Check for GORM duplicate key error
|
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
|
return true
|
|
}
|
|
|
|
// Check for PostgreSQL duplicate key error (SQLSTATE 23505)
|
|
errMsg := strings.ToLower(err.Error())
|
|
return strings.Contains(errMsg, "duplicate key value violates unique constraint") ||
|
|
strings.Contains(errMsg, "sqlstate 23505")
|
|
}
|