Skip to content

Commit

Permalink
Merge pull request #459 from traPtitech/refactor/db-errorhandling
Browse files Browse the repository at this point in the history
DBのエラーハンドリングをまとめる
  • Loading branch information
ras0q authored Sep 13, 2023
2 parents 2c1f0c1 + aeb97f7 commit d7d50e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
57 changes: 40 additions & 17 deletions infra/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
7 changes: 3 additions & 4 deletions infra/db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d7d50e6

Please sign in to comment.