Skip to content

Commit

Permalink
update: renamed ErrBy function to NewErr (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored Feb 5, 2023
1 parent ad36720 commit 14befeb
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 64 deletions.
32 changes: 16 additions & 16 deletions benchmark_err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ type /* error reason */ (
func _err_unused(v interface{}) {
}

func BenchmarkErr_ErrBy_empty(b *testing.B) {
func BenchmarkErr_NewErr_empty(b *testing.B) {
b.StartTimer()

for i := 0; i < b.N; i++ {
err := sabi.ErrBy(ReasonForBenchWithNoParam{})
err := sabi.NewErr(ReasonForBenchWithNoParam{})
_err_unused(err)
}

b.StopTimer()
}

func BenchmarkErr_ErrBy_manyParams(b *testing.B) {
func BenchmarkErr_NewErr_manyParams(b *testing.B) {
b.StartTimer()

for i := 0; i < b.N; i++ {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -51,7 +51,7 @@ func BenchmarkErr_ErrBy_manyParams(b *testing.B) {
}

func ProcForBenchByVal() sabi.Err {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -66,7 +66,7 @@ func ProcForBenchByVal() sabi.Err {
return err
}

func BenchmarkErr_ErrBy_byValue(b *testing.B) {
func BenchmarkErr_NewErr_byValue(b *testing.B) {
b.StartTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -78,7 +78,7 @@ func BenchmarkErr_ErrBy_byValue(b *testing.B) {
}

func ProcForBenchByPtr() *sabi.Err {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -93,7 +93,7 @@ func ProcForBenchByPtr() *sabi.Err {
return &err
}

func BenchmarkErr_ErrBy_byPtr(b *testing.B) {
func BenchmarkErr_NewErr_byPtr(b *testing.B) {
b.StartTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -105,7 +105,7 @@ func BenchmarkErr_ErrBy_byPtr(b *testing.B) {
}

func BenchmarkErr_Reason_emtpy(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithNoParam{})
err := sabi.NewErr(ReasonForBenchWithNoParam{})

b.StartTimer()

Expand All @@ -118,7 +118,7 @@ func BenchmarkErr_Reason_emtpy(b *testing.B) {
}

func BenchmarkErr_Reason_manyParams(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -142,7 +142,7 @@ func BenchmarkErr_Reason_manyParams(b *testing.B) {
}

func BenchmarkErr_Reason_type_emtpy(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithNoParam{})
err := sabi.NewErr(ReasonForBenchWithNoParam{})

b.StartTimer()

Expand All @@ -156,7 +156,7 @@ func BenchmarkErr_Reason_type_emtpy(b *testing.B) {
}

func BenchmarkErr_Reason_type_manyParams(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -181,7 +181,7 @@ func BenchmarkErr_Reason_type_manyParams(b *testing.B) {
}

func BenchmarkErr_ReasonName(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -206,7 +206,7 @@ func BenchmarkErr_ReasonName(b *testing.B) {

func BenchmarkErr_Cause(b *testing.B) {
cause := errors.New("Causal error")
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -230,7 +230,7 @@ func BenchmarkErr_Cause(b *testing.B) {
}

func BenchmarkErr_Situation(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand All @@ -254,7 +254,7 @@ func BenchmarkErr_Situation(b *testing.B) {
}

func BenchmarkErr_Get(b *testing.B) {
err := sabi.ErrBy(ReasonForBenchWithManyParams{
err := sabi.NewErr(ReasonForBenchWithManyParams{
Param1: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Param2: "abcdefghijklmnopqrstuvwxyz",
Param3: "8b91114c-f620-46bc-a991-25c7ac0f7935",
Expand Down
6 changes: 3 additions & 3 deletions dax.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (base *DaxBase) GetDaxConn(name string) (DaxConn, Err) {
ds = globalDaxSrcMap[name]
}
if ds == nil {
return nil, ErrBy(DaxSrcIsNotFound{Name: name})
return nil, NewErr(DaxSrcIsNotFound{Name: name})
}

base.daxConnMutex.Lock()
Expand All @@ -138,7 +138,7 @@ func (base *DaxBase) GetDaxConn(name string) (DaxConn, Err) {
var err Err
conn, err = ds.CreateDaxConn()
if !err.IsOk() {
return nil, ErrBy(FailToCreateDaxConn{Name: name}, err)
return nil, NewErr(FailToCreateDaxConn{Name: name}, err)
}

base.daxConnMap[name] = conn
Expand Down Expand Up @@ -179,7 +179,7 @@ func (base *DaxBase) commit() Err {
}

if len(errs) > 0 {
return ErrBy(FailToCommitDaxConn{Errors: errs})
return NewErr(FailToCommitDaxConn{Errors: errs})
}

return Ok()
Expand Down
4 changes: 2 additions & 2 deletions dax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type FooDaxConn struct {

func (conn *FooDaxConn) Commit() Err {
if WillFailToCommitFooDaxConn {
return ErrBy(InvalidDaxConn{})
return NewErr(InvalidDaxConn{})
}
logs.PushBack("FooDaxConn#Commit")
return Ok()
Expand All @@ -51,7 +51,7 @@ type FooDaxSrc struct {

func (ds FooDaxSrc) CreateDaxConn() (DaxConn, Err) {
if WillFailToCreateFooDaxConn {
return nil, ErrBy(InvalidDaxConn{})
return nil, NewErr(InvalidDaxConn{})
}
return &FooDaxConn{Label: ds.Label}, Ok()
}
Expand Down
10 changes: 5 additions & 5 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The following code is an example which implements two methods: GetName and Say w
func (dax mapDax) GetName() (string, sabi.Err) {
name, exists := dax.m["name"]
if !exists {
return "", sabi.ErrBy(NoName{})
return "", sabi.NewErr(NoName{})
}
return name, sabi.Ok()
}
Expand Down Expand Up @@ -88,7 +88,7 @@ This dax outputs a greeting to standard output.
func (dax SayConsoleDax) Say(text string) sabi.Err {
_, e := fmt.Println(text)
if e != nil {
return sabi.ErrBy(FailToPrint{}, e)
return sabi.NewErr(FailToPrint{}, e)
}
return sabi.Ok()
}
Expand All @@ -113,17 +113,17 @@ This dax accesses to a database and provides an implementation of GetName method
}
stmt, err := conn.Prepare("SELECT username FROM users LIMIT 1")
if err != nil {
return "", sabi.ErrBy(FailToCreateStmt{})
return "", sabi.NewErr(FailToCreateStmt{})
}
defer stmt.Close()
var username string
err = stmt.QueryRow().Scan(&username)
switch {
case err == sql.ErrNoRows:
return "", sabi.ErrBy(NoUser{})
return "", sabi.NewErr(NoUser{})
case err != nil:
return "", sabi.ErrBy(FailToQueryUserName{})
return "", sabi.NewErr(FailToQueryUserName{})
default:
return username, sabi.Ok()
}
Expand Down
4 changes: 2 additions & 2 deletions err.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func Ok() Err {
return ok
}

// ErrBy is a function which creates a new Err value with a reason and a cause.
// NewErr is a function which creates a new Err value with a reason and a cause.
// A reason is a structure type of which name expresses what is a reason.
func ErrBy(reason any, cause ...error) Err {
func NewErr(reason any, cause ...error) Err {
var err Err
err.reason = reason

Expand Down
18 changes: 9 additions & 9 deletions err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type /* error reasons */ (
}
)

func TestErrBy_reasonIsValue(t *testing.T) {
err := sabi.ErrBy(InvalidValue{Value: "abc"})
func TestNewErr_reasonIsValue(t *testing.T) {
err := sabi.NewErr(InvalidValue{Value: "abc"})

assert.Equal(t, err.Error(), "{reason=InvalidValue, Value=abc}")
assert.Equal(t, err.FileName(), "err_test.go")
Expand Down Expand Up @@ -45,8 +45,8 @@ func TestErrBy_reasonIsValue(t *testing.T) {
assert.Nil(t, err.Unwrap())
}

func TestErrBy_reasonIsPointer(t *testing.T) {
err := sabi.ErrBy(&InvalidValue{Value: "abc"})
func TestNewErr_reasonIsPointer(t *testing.T) {
err := sabi.NewErr(&InvalidValue{Value: "abc"})

assert.Equal(t, err.Error(), "{reason=InvalidValue, Value=abc}")
assert.Equal(t, err.FileName(), "err_test.go")
Expand Down Expand Up @@ -74,9 +74,9 @@ func TestErrBy_reasonIsPointer(t *testing.T) {
assert.Nil(t, err.Unwrap())
}

func TestErrBy_withCause(t *testing.T) {
func TestNewErr_withCause(t *testing.T) {
cause := errors.New("def")
err := sabi.ErrBy(InvalidValue{Value: "abc"}, cause)
err := sabi.NewErr(InvalidValue{Value: "abc"}, cause)

assert.Equal(t, err.Error(), "{reason=InvalidValue, Value=abc, cause=def}")
assert.Equal(t, err.FileName(), "err_test.go")
Expand Down Expand Up @@ -104,9 +104,9 @@ func TestErrBy_withCause(t *testing.T) {
assert.Equal(t, errors.Unwrap(err), cause)
}

func TestErrBy_causeIsAlsoErr(t *testing.T) {
cause := sabi.ErrBy(FailToGetValue{Name: "foo"})
err := sabi.ErrBy(InvalidValue{Value: "abc"}, cause)
func TestNewErr_causeIsAlsoErr(t *testing.T) {
cause := sabi.NewErr(FailToGetValue{Name: "foo"})
err := sabi.NewErr(InvalidValue{Value: "abc"}, cause)

assert.Equal(t, err.Error(), "{reason=InvalidValue, Value=abc, cause={reason=FailToGetValue, Name=foo}}")
assert.Equal(t, err.FileName(), "err_test.go")
Expand Down
Loading

0 comments on commit 14befeb

Please sign in to comment.