Skip to content

Commit 2179013

Browse files
authored
Merge pull request #31 from xgodev/bugfix/23-mongo-error-handling
Fix #23: Melhorar tratamento de erros na factory do MongoDB
2 parents 7082cf7 + 0235c29 commit 2179013

File tree

2 files changed

+14
-7
lines changed
  • factory/contrib/go.mongodb.org/mongo-driver/v1

2 files changed

+14
-7
lines changed

factory/contrib/go.mongodb.org/mongo-driver/v1/conn.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mongo
22

33
import (
44
"context"
5+
"github.com/xgodev/boost/model/errors"
56
"github.com/xgodev/boost/wrapper/log"
67
"strings"
78

@@ -36,7 +37,8 @@ func NewConn(ctx context.Context, plugins ...Plugin) (*Conn, error) {
3637

3738
o, err := NewOptions()
3839
if err != nil {
39-
logger.Fatalf(err.Error())
40+
logger.Errorf("Failed to get default options: %v", err)
41+
return nil, errors.NewInternal(err, "failed to get default options")
4042
}
4143

4244
return NewConnWithOptions(ctx, o, plugins...)
@@ -71,12 +73,14 @@ func NewConnWithOptions(ctx context.Context, o *Options, plugins ...Plugin) (con
7173

7274
co, err := clientOptions(ctx, o)
7375
if err != nil {
74-
logger.Fatalf(err.Error())
76+
logger.Errorf("Failed to create client options: %v", err)
77+
return nil, errors.NewInternal(err, "failed to create client options")
7578
}
7679

7780
for _, clientOptionsPlugin := range clientOptionsPlugins {
7881
if err := clientOptionsPlugin(ctx, co); err != nil {
79-
logger.Fatalf(err.Error())
82+
logger.Errorf("Failed to apply client options plugin: %v", err)
83+
return nil, errors.NewInternal(err, "failed to apply client options plugin")
8084
}
8185
}
8286

@@ -90,7 +94,8 @@ func NewConnWithOptions(ctx context.Context, o *Options, plugins ...Plugin) (con
9094

9195
for _, clientPlugin := range clientPlugins {
9296
if err := clientPlugin(ctx, client); err != nil {
93-
logger.Fatalf(err.Error())
97+
logger.Errorf("Failed to apply client plugin: %v", err)
98+
return nil, errors.NewInternal(err, "failed to apply client plugin")
9499
}
95100
}
96101

factory/contrib/go.mongodb.org/mongo-driver/v1/plugins/local/health/health.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package health
33
import (
44
"context"
55
"github.com/xgodev/boost/factory/contrib/go.mongodb.org/mongo-driver/v1"
6+
"github.com/xgodev/boost/model/errors"
67

78
"github.com/xgodev/boost/extra/health"
89
"github.com/xgodev/boost/wrapper/log"
@@ -29,12 +30,13 @@ func NewHealthWithConfigPath(path string) (*Health, error) {
2930
}
3031

3132
// NewHealth returns a new health with default options.
32-
func NewHealth() *Health {
33+
func NewHealth() (*Health, error) {
3334
o, err := NewOptions()
3435
if err != nil {
35-
log.Fatalf(err.Error())
36+
log.Errorf("Failed to get health options: %v", err)
37+
return nil, errors.NewInternal(err, "failed to get health options")
3638
}
37-
return NewHealthWithOptions(o)
39+
return NewHealthWithOptions(o), nil
3840
}
3941

4042
// Register registers this health plugin on a new mongo client.

0 commit comments

Comments
 (0)