Skip to content
Closed
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
5 changes: 4 additions & 1 deletion errors/err_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/pubgo/funk/proto/errorpb"
"github.com/pubgo/funk/stack"
"github.com/samber/lo"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
Expand Down Expand Up @@ -45,7 +47,8 @@ func NewCodeErrWithMsg(code *errorpb.ErrCode, msg string, details ...proto.Messa
return nil
}

code.Message = strings.ToTitle(strings.TrimSpace(msg))
caser := cases.Title(language.English)
code.Message = caser.String(strings.TrimSpace(msg))
Comment on lines +50 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating a caser with cases.Title is a relatively expensive operation. The documentation for golang.org/x/text/cases recommends creating it once and reusing it. To improve performance, consider defining this caser as a package-level variable to avoid creating it on every function call.

return NewCodeErr(code, details...)
}

Expand Down
3 changes: 2 additions & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errors
import (
"errors"
"fmt"
"os"
"reflect"
"runtime/debug"

Expand Down Expand Up @@ -60,7 +61,7 @@ func Debug(err error) {

err = parseError(err)
if _err, ok := err.(fmt.Stringer); ok {
fmt.Println(_err.String())
_, _ = fmt.Fprintln(os.Stderr, _err.String())
return
}

Expand Down
10 changes: 10 additions & 0 deletions errors/z_err_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ func TestErrCode(t *testing.T) {

t.Log(errors.As(err1, testcodepb.TestErrCodeDbConn))
}

func TestNewCodeErrWithMsg(t *testing.T) {
err1 := errors.NewCodeErrWithMsg(testcodepb.TestErrCodeDbConn, "hello world")
var err2 *errors.ErrCode
t.Log(errors.As(err1, &err2))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The test should check the boolean return value of errors.As instead of just logging it. If errors.As fails, err2 will be nil, and the subsequent call to err2.Error() on the next line will cause a panic, making the test fragile.

if !errors.As(err1, �&err2) {
	t.Fatal("errors.As failed to cast to *errors.ErrCode")
}

if err2.Error() != "Hello World" {
t.Fatal("message not match")
}
t.Log(err2.Error())
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/pubgo/funk

go 1.22

replace github.com/ugorji/go/codec => github.com/ugorji/go/codec v1.2.7

require (
dario.cat/mergo v1.0.0
entgo.io/ent v0.13.1
Expand Down