IsUniqueConstraintError reports if the error resulted from a DB uniqueness constraint violation. e.g. duplicate value in unique index.
(err error)
| 21 | // IsUniqueConstraintError reports if the error resulted from a DB uniqueness constraint violation. |
| 22 | // e.g. duplicate value in unique index. |
| 23 | func IsUniqueConstraintError(err error) bool { |
| 24 | if err == nil { |
| 25 | return false |
| 26 | } |
| 27 | for _, s := range []string{ |
| 28 | "Error 1062", // MySQL |
| 29 | "violates unique constraint", // Postgres |
| 30 | "UNIQUE constraint failed", // SQLite |
| 31 | } { |
| 32 | if strings.Contains(err.Error(), s) { |
| 33 | return true |
| 34 | } |
| 35 | } |
| 36 | return false |
| 37 | } |
| 38 | |
| 39 | // IsForeignKeyConstraintError reports if the error resulted from a database foreign-key constraint violation. |
| 40 | // e.g. parent row does not exist. |
searching dependent graphs…