Skip to content

Commit

Permalink
Fixed error message when dialector fails to initialize (#6509)
Browse files Browse the repository at this point in the history
Let's say we have a problem with DSN which leads to dialector initialize error. However DB connection is not created and for some reason line 184 error provides <nil> even though "db" doesn't exist.

Previously, this code leads to:
panic: runtime error: invalid memory address or nil pointer dereference

This fix now doesn't attempt to close non-existant database connection and instead continues, so the proper error is shown. In my case:
[error] failed to initialize database, got error default addr for network 'localhost' unknown
  • Loading branch information
RatajVaver committed Aug 20, 2023
1 parent 7e44f73 commit ac07543
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion gorm.go
Expand Up @@ -182,7 +182,7 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
err = config.Dialector.Initialize(db)

if err != nil {
if db, err := db.DB(); err == nil {
if db, _ := db.DB(); db != nil {
_ = db.Close()
}
}
Expand Down

0 comments on commit ac07543

Please sign in to comment.