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:
if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil { |
Or alternatively:
if result := db.Where("name = ?", "jinzhu").First(&user); result.Error != nil { |
ErrRecordNotFound
GORM returns ErrRecordNotFound
when no record is found using methods like First
, Last
, Take
.
err := db.First(&user, 100).Error |
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
import ( |
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.
db, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{TranslateError: true}) |
- ErrDuplicatedKey
This error occurs when an insert operation violates a unique constraint:
result := db.Create(&newRecord) |
- ErrForeignKeyViolated
This error is encountered when a foreign key constraint is violated:
result := db.Create(&newRecord) |
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.