Effective error handling is a cornerstone of robust application development in Go, particularly when interacting with databases using GORM. GORM’s approach to error handling, influenced by its chainable API, requires a nuanced understanding.

Basic Error Handling

GORM integrates error handling into its chainable method syntax. The *gorm.DB instance contains an Error field, which is set when an error occurs. The common practice is to check this field after executing database operations, especially after Finisher Methods.

After a chain of methods, it’s crucial to check the Error field:

  1. if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
  2. // Handle error...
  3. }

Or alternatively:

  1. if result := db.Where("name = ?", "jinzhu").First(&user); result.Error != nil {
  2. // Handle error...
  3. }

ErrRecordNotFound

GORM returns ErrRecordNotFound when no record is found using methods like First, Last, Take.

  1. err := db.First(&user, 100).Error
  2. if errors.Is(err, gorm.ErrRecordNotFound) {
  3. // Handle record not found error...
  4. }

Handling Error Codes

Many databases return errors with specific codes, which can be indicative of various issues like constraint violations, connection problems, or syntax errors. Handling these error codes in GORM requires parsing the error returned by the database and extracting the relevant code

  • Example: Handling MySQL Error Codes
  1. import (
  2. "github.com/go-sql-driver/mysql"
  3. "gorm.io/gorm"
  4. )
  5. // ...
  6. result := db.Create(&newRecord)
  7. if result.Error != nil {
  8. if mysqlErr, ok := result.Error.(*mysql.MySQLError); ok {
  9. switch mysqlErr.Number {
  10. case 1062: // MySQL code for duplicate entry
  11. // Handle duplicate entry
  12. // Add cases for other specific error codes
  13. default:
  14. // Handle other errors
  15. }
  16. } else {
  17. // Handle non-MySQL errors or unknown errors
  18. }
  19. }

Dialect Translated Errors

GORM can return specific errors related to the database dialect being used, when TranslateError is enabled, GORM converts database-specific errors into its own generalized errors.

  1. db, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{TranslateError: true})
  • ErrDuplicatedKey

This error occurs when an insert operation violates a unique constraint:

  1. result := db.Create(&newRecord)
  2. if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
  3. // Handle duplicated key error...
  4. }
  • ErrForeignKeyViolated

This error is encountered when a foreign key constraint is violated:

  1. result := db.Create(&newRecord)
  2. if errors.Is(result.Error, gorm.ErrForeignKeyViolated) {
  3. // Handle foreign key violation error...
  4. }

By enabling TranslateError, GORM provides a more unified way of handling errors across different databases, translating database-specific errors into common GORM error types.

Errors

For a complete list of errors that GORM can return, refer to the Errors List in GORM’s documentation.