From c409ce82c1ec8e2aea2af7c22b3a62530ab41cd8 Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Wed, 25 Dec 2024 10:02:18 +0800 Subject: [PATCH] chore: Optimize lanuch error message when database is disabled (#785) * chore: Optimize lanuch error message when database is disabled * remove unnecessary code --- auth/service_provider.go | 3 ++- database/gorm/dialector.go | 2 +- database/gorm/dialector_test.go | 2 +- database/migration/sql_migrator.go | 2 +- database/service_provider.go | 12 ++++++++++-- errors/list.go | 2 +- foundation/container.go | 9 +++++++++ support/color/color.go | 10 +++++++++- 8 files changed, 34 insertions(+), 8 deletions(-) diff --git a/auth/service_provider.go b/auth/service_provider.go index 7d6d6310a..f3696b17e 100644 --- a/auth/service_provider.go +++ b/auth/service_provider.go @@ -32,7 +32,8 @@ func (database *ServiceProvider) Register(app foundation.Application) { ormFacade := app.MakeOrm() if ormFacade == nil { - return nil, errors.OrmFacadeNotSet.SetModule(errors.ModuleAuth) + // The Orm module will print the error message, so it's safe to return nil. + return nil, nil } ctx, ok := parameters["ctx"].(http.Context) diff --git a/database/gorm/dialector.go b/database/gorm/dialector.go index 8e462ee5a..7f096b0bc 100644 --- a/database/gorm/dialector.go +++ b/database/gorm/dialector.go @@ -19,7 +19,7 @@ func getDialectors(configs []database.FullConfig) ([]gorm.Dialector, error) { var dialector gorm.Dialector dsn := db.Dsn(config) if dsn == "" { - return nil, errors.OrmFailedToGenerateDNS.Args(config.Connection) + return nil, errors.OrmFailedToGenerateDNS } switch config.Driver { diff --git a/database/gorm/dialector_test.go b/database/gorm/dialector_test.go index 566c20071..261694aa7 100644 --- a/database/gorm/dialector_test.go +++ b/database/gorm/dialector_test.go @@ -32,7 +32,7 @@ func TestGetDialectors(t *testing.T) { Connection: "postgres", }, }, - expectError: errors.OrmFailedToGenerateDNS.Args("postgres"), + expectError: errors.OrmFailedToGenerateDNS, }, { name: "Happy path - mysql", diff --git a/database/migration/sql_migrator.go b/database/migration/sql_migrator.go index 1bc72ce5e..6a8ceaabc 100644 --- a/database/migration/sql_migrator.go +++ b/database/migration/sql_migrator.go @@ -148,7 +148,7 @@ func getMigrator(configBuilder *databasedb.ConfigBuilder, table string) (*migrat writeConfig := writeConfigs[0] dsn := databasedb.Dsn(writeConfigs[0]) if dsn == "" { - return nil, errors.OrmFailedToGenerateDNS.Args(writeConfig.Connection) + return nil, errors.OrmFailedToGenerateDNS } var ( diff --git a/database/service_provider.go b/database/service_provider.go index a053c6a6f..1103e6d1e 100644 --- a/database/service_provider.go +++ b/database/service_provider.go @@ -13,6 +13,7 @@ import ( databaseschema "github.com/goravel/framework/database/schema" databaseseeder "github.com/goravel/framework/database/seeder" "github.com/goravel/framework/errors" + "github.com/goravel/framework/support/color" ) type ServiceProvider struct { @@ -32,9 +33,15 @@ func (r *ServiceProvider) Register(app foundation.Application) { } connection := config.GetString("database.default") + if connection == "" { + return nil, nil + } + orm, err := databaseorm.BuildOrm(ctx, config, connection, log, app.Refresh) if err != nil { - return nil, errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm) + color.Warningln(errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm)) + + return nil, nil } return orm, nil @@ -52,7 +59,8 @@ func (r *ServiceProvider) Register(app foundation.Application) { orm := app.MakeOrm() if orm == nil { - return nil, errors.OrmFacadeNotSet.SetModule(errors.ModuleSchema) + // The Orm module will print the error message, so it's safe to return an empty schema. + return &databaseschema.Schema{}, nil } return databaseschema.NewSchema(config, log, orm, nil), nil diff --git a/errors/list.go b/errors/list.go index 8b1971d93..8ff6b5271 100644 --- a/errors/list.go +++ b/errors/list.go @@ -87,7 +87,7 @@ var ( OrmDatabaseConfigNotFound = New("not found database configuration") OrmDriverNotSupported = New("invalid driver: %s, only support mysql, postgres, sqlite and sqlserver") - OrmFailedToGenerateDNS = New("failed to generate DSN for connection: %s") + OrmFailedToGenerateDNS = New("failed to generate DSN, please check the database configuration") OrmFactoryMissingAttributes = New("failed to get raw attributes") OrmFactoryMissingMethod = New("%s does not find factory method") OrmInitConnection = New("init %s connection error: %v") diff --git a/foundation/container.go b/foundation/container.go index ff5af7416..cedf52b3b 100644 --- a/foundation/container.go +++ b/foundation/container.go @@ -102,6 +102,9 @@ func (c *Container) MakeAuth(ctx contractshttp.Context) contractsauth.Auth { color.Errorln(err) return nil } + if instance == nil { + return nil + } return instance.(contractsauth.Auth) } @@ -214,6 +217,9 @@ func (c *Container) MakeOrm() contractsorm.Orm { color.Errorln(err) return nil } + if instance == nil { + return nil + } return instance.(contractsorm.Orm) } @@ -264,6 +270,9 @@ func (c *Container) MakeSchema() contractsmigration.Schema { color.Errorln(err) return nil } + if instance == nil { + return nil + } return instance.(contractsmigration.Schema) } diff --git a/support/color/color.go b/support/color/color.go index fa08c1ed4..7913497e7 100644 --- a/support/color/color.go +++ b/support/color/color.go @@ -3,6 +3,7 @@ package color import ( "bytes" "io" + "os" "github.com/pterm/pterm" @@ -38,7 +39,14 @@ const ( ) var ( - info = pterm.Info + info = pterm.PrefixPrinter{ + MessageStyle: &pterm.ThemeDefault.DefaultText, + Prefix: pterm.Prefix{ + Style: &pterm.Style{pterm.FgBlack, pterm.BgLightWhite}, + Text: " INFO ", + }, + Writer: os.Stdout, + } warning = pterm.Warning err = pterm.Error debug = pterm.Debug