Go试图处理错误而不是我。

huangapple go评论90阅读模式
英文:

Go tries to handle an error instead of me

问题

我正在使用GORM运行以下代码:

result := db.Where("account_name = ?", config.AccountName).First(&accountRecord) // line 299

// 如果在与数据库交互时出现错误
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
    fmt.Println("处理accountResponse时出错")
    return 0, false, result.Error
}

// 如果记录存在
if result.Error != nil {
    accountRecord = newAcountData

    result = db.Create(&accountRecord)
} else {
    result = db.Model(&accountRecord).Updates(newAcountData)
}

我已经在找不到记录的情况下运行了一些逻辑。然而,错误仍然显示在我的控制台上,显示为The record was not found

2021/08/30 18:30:16 /Users/kana/projects/server/data-processor/commands.go:289 找不到记录
[4.966ms] [rows:0] SELECT * FROM "accounts" WHERE account_name = 'kana' AND "accounts"."deleted_at" IS NULL ORDER BY "accounts"."id" LIMIT 1

为什么会发生这种情况?整个程序在此之后运行良好,没有崩溃,整体行为与设计一致。只是这个错误消息让我感到烦恼。

英文:

I have the following code running using GORM:

result := db.Where("account_name = ?", config.AccountName).First(&accountRecord) // line 299

	// if there is some error when working with DB
	if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
		fmt.Println("error when processing accountResponse")
		return 0, false, result.Error
	}

// in case that record exists
	if result.Error != nil {
		accountRecord = newAcountData

		result = db.Create(&accountRecord)
	} else {
		result = db.Model(&accountRecord).Updates(newAcountData)
	}

I already do want to run some logic in the case where the record was not found. However - the error is still shown in my console saying The record was not found:

2021/08/30 18:30:16 /Users/kana/projects/server/data-processor/commands.go:289 record not found
[4.966ms] [rows:0] SELECT * FROM "accounts" WHERE account_name = 'kana' AND "accounts"."deleted_at" IS NULL ORDER BY "accounts"."id" LIMIT 1

Why does that happen? The whole program runs fine after that - nothing crashes and the overall behavior is the way it was designed. Simply this error message annoys me.

答案1

得分: 2

这是因为 GORM 处理了一些日志输出,其中之一就是 "record not found"。如果你需要禁用这些类型的日志活动,可以按照以下步骤操作:

如果你使用的是 GORM v1

https://pkg.go.dev/github.com/jinzhu/gorm#DB.LogMode

db.LogMode(false)

如果你使用的是 GORM v2,你可以将 logger 传递给 gorm 配置并控制输出。

https://gorm.io/docs/logger.html#Log-Levels

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
  Logger: logger.Default.LogMode(logger.Silent),
})
英文:

This is because the GORM is handling some log outputs and record not found is one of them. If you need to disable these kind of log activities you can do the followings:

if you are using GORM v1

https://pkg.go.dev/github.com/jinzhu/gorm#DB.LogMode

db.LogMode(false)

if you are using GORM v2 you can pass logger to gorm config and control the outputs.

https://gorm.io/docs/logger.html#Log-Levels

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
  Logger: logger.Default.LogMode(logger.Silent),
})

huangapple
  • 本文由 发表于 2021年8月30日 20:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/68984255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定