Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't panic when the database connection cannot be established a… #77

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (m Module) ProvideCommand(command *cobra.Command) {
}
exportedConfigs = copy
}
os.MkdirAll(filepath.Dir(outputFile), 0644)
os.MkdirAll(filepath.Dir(outputFile), os.ModePerm)
targetFile, err = os.OpenFile(outputFile,
handler.flags(), 0644)
handler.flags(), os.ModePerm)
if err != nil {
return errors.Wrap(err, "failed to open config file")
}
Expand Down
8 changes: 7 additions & 1 deletion otgorm/dependency.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package otgorm

import (
"errors"
"fmt"
"net"

"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
Expand Down Expand Up @@ -160,9 +162,13 @@ func provideGormConfig(l log.Logger, conf *databaseConf) *gorm.Config {
// will fail when initializing dependencies.
func provideGormDB(dialector gorm.Dialector, config *gorm.Config, tracer opentracing.Tracer) (*gorm.DB, func(), error) {
db, err := gorm.Open(dialector, config)
if err != nil {

var nerr *net.OpError

if err != nil && !errors.As(err, &nerr) {
return nil, nil, err
}

if tracer != nil {
AddGormCallbacks(db, tracer)
}
Expand Down
6 changes: 3 additions & 3 deletions otgorm/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func TestProvideDBFactory(t *testing.T) {
Conf: config.MapAdapter{"gorm": map[string]databaseConf{
"default": {
Database: "sqlite",
Dsn: "",
Dsn: ":memory:",
},
"alternative": {
Database: "sqlite",
Dsn: "",
Database: "mysql",
Dsn: "root@tcp(127.0.0.1:3306)/app?charset=utf8mb4&parseTime=True&loc=Local",
},
}},
Logger: log.NewNopLogger(),
Expand Down