From a6954f18efdca5b0687145cf4884a489278fc103 Mon Sep 17 00:00:00 2001 From: ras0q Date: Wed, 13 Sep 2023 21:03:32 +0900 Subject: [PATCH 1/2] :recycle: use custom dialector --- infra/db/db.go | 57 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/infra/db/db.go b/infra/db/db.go index b5c6a868..97b1ee75 100644 --- a/infra/db/db.go +++ b/infra/db/db.go @@ -37,24 +37,28 @@ func (repo *GormRepository) Setup(host, user, password, database, port, key, log } tokenKey = []byte(key) + d := dialector{ + Dialector: mysql.New(mysql.Config{ + DSNConfig: &gomysql.Config{ + User: user, + Passwd: password, + Net: "tcp", + Addr: host + ":" + port, + DBName: database, + Loc: loc, + AllowNativePasswords: true, + ParseTime: true, + }, + DefaultStringSize: 256, // default size for string fields + DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6 + DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB + DontSupportRenameColumn: false, // `change` when rename column, rename column not supported before MySQL 8, MariaDB + SkipInitializeWithVersion: false, // auto configure based on currently MySQL version + }), + } + var err error - repo.db, err = gorm.Open(mysql.New(mysql.Config{ - DSNConfig: &gomysql.Config{ - User: user, - Passwd: password, - Net: "tcp", - Addr: host + ":" + port, - DBName: database, - Loc: loc, - AllowNativePasswords: true, - ParseTime: true, - }, - DefaultStringSize: 256, // default size for string fields - DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6 - DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB - DontSupportRenameColumn: false, // `change` when rename column, rename column not supported before MySQL 8, MariaDB - SkipInitializeWithVersion: false, // auto configure based on currently MySQL version - }), &gorm.Config{ + repo.db, err = gorm.Open(d, &gorm.Config{ Logger: logger.Default.LogMode(loglevel), }) if err != nil { @@ -63,3 +67,22 @@ func (repo *GormRepository) Setup(host, user, password, database, port, key, log return migration.Migrate(repo.db, tables) } + +// dialector with custom error handling +type dialector struct { + gorm.Dialector +} + +var ( + _ gorm.Dialector = (*dialector)(nil) + _ gorm.ErrorTranslator = (*dialector)(nil) +) + +// override Translate(err error) error +func (d dialector) Translate(err error) error { + if translater, ok := d.Dialector.(gorm.ErrorTranslator); ok { + err = translater.Translate(err) + } + + return defaultErrorHandling(err) +} From aeb97f7b35970069271efc1e5b33cfdbed4404cc Mon Sep 17 00:00:00 2001 From: ras0q Date: Wed, 13 Sep 2023 21:04:35 +0900 Subject: [PATCH 2/2] :recycle: wrap child error --- infra/db/errors.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/infra/db/errors.go b/infra/db/errors.go index c1b9e58e..aee4a7f7 100644 --- a/infra/db/errors.go +++ b/infra/db/errors.go @@ -39,17 +39,16 @@ var ( // defaultErrorHandling mysql等のエラーをハンドリングする // テストと連携して、いい感じに変換する -// TODO wrapして上層に伝えたい func defaultErrorHandling(err error) error { var me *mysql.MySQLError if errors.As(err, &me) { switch me.Number { case 1032: - return ErrInvalidArgs + return fmt.Errorf("%w: %w", ErrInvalidArgs, err) case 1062: - return ErrDuplicateEntry + return fmt.Errorf("%w: %w", ErrDuplicateEntry, err) case 1452: - return ErrRecordNotFound + return fmt.Errorf("%w: %w", ErrRecordNotFound, err) } } return err