Skip to content

Commit

Permalink
🔄 Sync from monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
mojo-machine[bot] committed Oct 9, 2024
1 parent 69cc709 commit cd5ffbe
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 216 deletions.
10 changes: 7 additions & 3 deletions lib/cher/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,18 @@ func WrapIfNotCher(err error, msg string) error {

// WrapIfNotCherCodes will wrap an error unless it is a cher with specific codes.
func WrapIfNotCherCodes(err error, msg string, codes []string) error {
if err == nil {
return nil
}
return WrapIfNotCherCode(err, msg, codes...)
}

func WrapIfNotCherCode(err error, msg string, codes ...string) error {
var cErr E
if errors.As(err, &cErr) && slices.Contains(codes, cErr.Code) {
return cErr
}

return errors.Wrap(err, msg)
}

func AsCherWithCode(err error, codes ...string) (cErr E, ok bool) {
return cErr, errors.As(err, &cErr) && slices.Contains(codes, cErr.Code)
}
72 changes: 72 additions & 0 deletions lib/cher/error_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cher

import (
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -207,3 +208,74 @@ func TestWrapIfNotCherCodes(t *testing.T) {
})
}
}

func TestAsCherWithCode(t *testing.T) {
type testCase struct {
name string
err error
codes []string
expect func(is *is.I, cErr E, ok bool)
}

tests := []testCase{
{
name: "nil",
err: nil,
codes: []string{"code_1"},
expect: func(is *is.I, cErr E, ok bool) {
is.Equal(ok, false)
},
},
{
name: "normal error",
err: fmt.Errorf("nope"), //nolint:forbidigo,err113 // required for test
codes: []string{"code_1"},
expect: func(is *is.I, cErr E, ok bool) {
is.Equal(ok, false)
},
},
{
name: "normal error with same string",
err: fmt.Errorf("code_1"), //nolint:forbidigo,err113 // required for test
codes: []string{"code_1"},
expect: func(is *is.I, cErr E, ok bool) {
is.Equal(ok, false)
},
},
{
name: "cher specified code",
err: New("code_1", nil),
codes: []string{"code_1"},
expect: func(is *is.I, cErr E, ok bool) {
is.True(ok)
is.Equal(cErr.Code, "code_1")
},
},
{
name: "cher other code",
err: New("unknown", nil),
codes: []string{"code_1"},
expect: func(is *is.I, cErr E, ok bool) {
is.Equal(ok, false)
},
},
{
name: "wrapped cher",
err: errors.Wrap(New("code_1", nil), "wrapped"),
codes: []string{"code_1"},
expect: func(is *is.I, cErr E, ok bool) {
is.True(ok)
is.Equal(cErr.Code, "code_1")
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
is := is.New(t)

cErr, ok := AsCherWithCode(tc.err, tc.codes...)
tc.expect(is, cErr, ok)
})
}
}
1 change: 1 addition & 0 deletions lib/clog/clog.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func SetError(ctx context.Context, err error) {

cherErr := cher.E{}
if errors.As(err, &cherErr) {
ctxLogger.SetField("error_code", cherErr.Code)
if len(cherErr.Reasons) > 0 {
ctxLogger.SetField("error_reasons", cherErr.Reasons)
}
Expand Down
187 changes: 0 additions & 187 deletions lib/config/common.go

This file was deleted.

24 changes: 24 additions & 0 deletions lib/config/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"context"
"os"
"os/signal"
"syscall"
)

func ContextWithCancelOnSignal(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

go func() {
defer cancel()
select {
case <-stop:
case <-ctx.Done():
}
}()

return ctx
}
73 changes: 73 additions & 0 deletions lib/config/mongodb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package config

import (
"context"
"time"

"github.com/wearemojo/mojo-public-go/lib/db/mongodb"
"github.com/wearemojo/mojo-public-go/lib/merr"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
)

// MongoDB configures a connection to a Mongo database.
type MongoDB struct {
URI string `json:"uri"`
ConnectTimeout time.Duration `json:"connect_timeout"`
MaxConnIdleTime *time.Duration `json:"max_conn_idle_time"`
MaxConnecting *uint64 `json:"max_connecting"`
MaxPoolSize *uint64 `json:"max_pool_size"`
MinPoolSize *uint64 `json:"min_pool_size"`
}

// Options returns the MongoDB client options and database name.
func (m MongoDB) Options(ctx context.Context) (opts *options.ClientOptions, dbName string, err error) {
opts = options.Client().ApplyURI(m.URI)
opts.MaxConnIdleTime = m.MaxConnIdleTime
opts.MaxConnecting = m.MaxConnecting
opts.MaxPoolSize = m.MaxPoolSize
opts.MinPoolSize = m.MinPoolSize

err = opts.Validate()
if err != nil {
return
}

// all Go services use majority reads/writes, and this is unlikely to change
// if it does change, switch to accepting as an argument
opts.SetReadConcern(readconcern.Majority())
opts.SetWriteConcern(writeconcern.Majority())

cs, err := connstring.Parse(m.URI)
if err != nil {
return
}

dbName = cs.Database
if dbName == "" {
err = merr.New(ctx, "mongo_db_name_missing", nil)
}

return
}

// Connect returns a connected mongo.Database instance.
func (m MongoDB) Connect(ctx context.Context) (*mongodb.Database, error) {
opts, dbName, err := m.Options(ctx)
if err != nil {
return nil, err
}

if m.ConnectTimeout == 0 {
m.ConnectTimeout = 10 * time.Second
}

// this package can only be used for service config
// so can only happen at init-time - no need to accept context input
ctx, cancel := context.WithTimeout(ctx, m.ConnectTimeout)
defer cancel()

return mongodb.Connect(ctx, opts, dbName)
}
Loading

0 comments on commit cd5ffbe

Please sign in to comment.