Skip to content

Commit c409ce8

Browse files
authored
chore: Optimize lanuch error message when database is disabled (#785)
* chore: Optimize lanuch error message when database is disabled * remove unnecessary code
1 parent 6b30537 commit c409ce8

8 files changed

Lines changed: 34 additions & 8 deletions

File tree

auth/service_provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func (database *ServiceProvider) Register(app foundation.Application) {
3232

3333
ormFacade := app.MakeOrm()
3434
if ormFacade == nil {
35-
return nil, errors.OrmFacadeNotSet.SetModule(errors.ModuleAuth)
35+
// The Orm module will print the error message, so it's safe to return nil.
36+
return nil, nil
3637
}
3738

3839
ctx, ok := parameters["ctx"].(http.Context)

database/gorm/dialector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func getDialectors(configs []database.FullConfig) ([]gorm.Dialector, error) {
1919
var dialector gorm.Dialector
2020
dsn := db.Dsn(config)
2121
if dsn == "" {
22-
return nil, errors.OrmFailedToGenerateDNS.Args(config.Connection)
22+
return nil, errors.OrmFailedToGenerateDNS
2323
}
2424

2525
switch config.Driver {

database/gorm/dialector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestGetDialectors(t *testing.T) {
3232
Connection: "postgres",
3333
},
3434
},
35-
expectError: errors.OrmFailedToGenerateDNS.Args("postgres"),
35+
expectError: errors.OrmFailedToGenerateDNS,
3636
},
3737
{
3838
name: "Happy path - mysql",

database/migration/sql_migrator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func getMigrator(configBuilder *databasedb.ConfigBuilder, table string) (*migrat
148148
writeConfig := writeConfigs[0]
149149
dsn := databasedb.Dsn(writeConfigs[0])
150150
if dsn == "" {
151-
return nil, errors.OrmFailedToGenerateDNS.Args(writeConfig.Connection)
151+
return nil, errors.OrmFailedToGenerateDNS
152152
}
153153

154154
var (

database/service_provider.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
databaseschema "github.com/goravel/framework/database/schema"
1414
databaseseeder "github.com/goravel/framework/database/seeder"
1515
"github.com/goravel/framework/errors"
16+
"github.com/goravel/framework/support/color"
1617
)
1718

1819
type ServiceProvider struct {
@@ -32,9 +33,15 @@ func (r *ServiceProvider) Register(app foundation.Application) {
3233
}
3334

3435
connection := config.GetString("database.default")
36+
if connection == "" {
37+
return nil, nil
38+
}
39+
3540
orm, err := databaseorm.BuildOrm(ctx, config, connection, log, app.Refresh)
3641
if err != nil {
37-
return nil, errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm)
42+
color.Warningln(errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm))
43+
44+
return nil, nil
3845
}
3946

4047
return orm, nil
@@ -52,7 +59,8 @@ func (r *ServiceProvider) Register(app foundation.Application) {
5259

5360
orm := app.MakeOrm()
5461
if orm == nil {
55-
return nil, errors.OrmFacadeNotSet.SetModule(errors.ModuleSchema)
62+
// The Orm module will print the error message, so it's safe to return an empty schema.
63+
return &databaseschema.Schema{}, nil
5664
}
5765

5866
return databaseschema.NewSchema(config, log, orm, nil), nil

errors/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ var (
8787

8888
OrmDatabaseConfigNotFound = New("not found database configuration")
8989
OrmDriverNotSupported = New("invalid driver: %s, only support mysql, postgres, sqlite and sqlserver")
90-
OrmFailedToGenerateDNS = New("failed to generate DSN for connection: %s")
90+
OrmFailedToGenerateDNS = New("failed to generate DSN, please check the database configuration")
9191
OrmFactoryMissingAttributes = New("failed to get raw attributes")
9292
OrmFactoryMissingMethod = New("%s does not find factory method")
9393
OrmInitConnection = New("init %s connection error: %v")

foundation/container.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func (c *Container) MakeAuth(ctx contractshttp.Context) contractsauth.Auth {
102102
color.Errorln(err)
103103
return nil
104104
}
105+
if instance == nil {
106+
return nil
107+
}
105108

106109
return instance.(contractsauth.Auth)
107110
}
@@ -214,6 +217,9 @@ func (c *Container) MakeOrm() contractsorm.Orm {
214217
color.Errorln(err)
215218
return nil
216219
}
220+
if instance == nil {
221+
return nil
222+
}
217223

218224
return instance.(contractsorm.Orm)
219225
}
@@ -264,6 +270,9 @@ func (c *Container) MakeSchema() contractsmigration.Schema {
264270
color.Errorln(err)
265271
return nil
266272
}
273+
if instance == nil {
274+
return nil
275+
}
267276

268277
return instance.(contractsmigration.Schema)
269278
}

support/color/color.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package color
33
import (
44
"bytes"
55
"io"
6+
"os"
67

78
"github.com/pterm/pterm"
89

@@ -38,7 +39,14 @@ const (
3839
)
3940

4041
var (
41-
info = pterm.Info
42+
info = pterm.PrefixPrinter{
43+
MessageStyle: &pterm.ThemeDefault.DefaultText,
44+
Prefix: pterm.Prefix{
45+
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightWhite},
46+
Text: " INFO ",
47+
},
48+
Writer: os.Stdout,
49+
}
4250
warning = pterm.Warning
4351
err = pterm.Error
4452
debug = pterm.Debug

0 commit comments

Comments
 (0)