From dfcb1bccf1257a41197d077a8381257e3a8a8f78 Mon Sep 17 00:00:00 2001 From: Tabuci Octavian Date: Wed, 29 Mar 2023 11:34:46 +0300 Subject: [PATCH] db client error codes --- core/clients/db/dberr/codes.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 core/clients/db/dberr/codes.go diff --git a/core/clients/db/dberr/codes.go b/core/clients/db/dberr/codes.go new file mode 100644 index 0000000..9cea769 --- /dev/null +++ b/core/clients/db/dberr/codes.go @@ -0,0 +1,22 @@ +package dberr + +import ( + "errors" + "github.com/go-sql-driver/mysql" + "github.com/jackc/pgx/v5/pgconn" +) + +func IsDuplicateKey(_err error) bool { + if pgError := _err.(*pgconn.PgError); errors.Is(_err, pgError) { + switch pgError.Code { + case "23505": + return true + } + } else if mysqlErr := _err.(*mysql.MySQLError); errors.Is(_err, mysqlErr) { + switch mysqlErr.Number { + case 1062: + return true + } + } + return false +}