Skip to content

Commit

Permalink
fix: go.mod versioning & errors
Browse files Browse the repository at this point in the history
  • Loading branch information
uknth committed Oct 16, 2023
1 parent 3f5e561 commit be72912
Show file tree
Hide file tree
Showing 36 changed files with 207 additions and 286 deletions.
6 changes: 3 additions & 3 deletions data/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"time"

"github.com/unbxd/go-base/data/cache/inmem"
"github.com/unbxd/go-base/data/cache/redis"
"github.com/unbxd/go-base/log"
"github.com/unbxd/go-base/v2/data/cache/inmem"
"github.com/unbxd/go-base/v2/data/cache/redis"
"github.com/unbxd/go-base/v2/log"
)

type Cache interface {
Expand Down
2 changes: 1 addition & 1 deletion data/cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

redis "github.com/redis/go-redis/v9"
"github.com/unbxd/go-base/log"
"github.com/unbxd/go-base/v2/log"
)

var NOEXPIRE = time.Duration(0)
Expand Down
4 changes: 2 additions & 2 deletions data/driver/zook/zook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"

"github.com/samuel/go-zookeeper/zk"
"github.com/unbxd/go-base/data/driver"
"github.com/unbxd/go-base/errors"
"github.com/unbxd/go-base/v2/data/driver"
"github.com/unbxd/go-base/v2/errors"
)

type (
Expand Down
86 changes: 78 additions & 8 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,91 @@ import (
"fmt"
)

type causer interface {
Cause() error
}

type withCause struct {
cause error
msg string
}

func (wc *withCause) Cause() error { return wc.cause }
func (wc *withCause) Unwrap() error { return wc.cause }
func (wc *withCause) Error() string { return fmt.Sprintf("%s: %s", wc.msg, wc.cause.Error()) }

// With is easy error access
func With(err error, errors ...error) error {
return builtin_errors.Join(append([]error{err}, errors...)...)
func With(err error, errs ...error) error {
return WithMessage(err, "causes", errs...)
}

func WithMessage(err error, msg string, errs ...error) error {
return builtin_errors.Join(
append(
[]error{&withCause{
err, msg,
}},
errs...,
)...,
)
}

// Builtin Methods for Errors Package
func Is(err, target error) bool { return builtin_errors.Is(err, target) }
func Is(err, target error) bool { return builtin_errors.Is(err, target) }

// TODO: As test cases
func As(err error, target any) bool { return builtin_errors.As(err, target) }
func Join(errors ...error) error { return builtin_errors.Join(errors...) }
func Unwrap(err error) error { return builtin_errors.Unwrap(err) }
func New(msg string) error { return builtin_errors.New(msg) }

// Method from github.com/unbxd/go-base/errors
func Wrap(err error, str string) error { return fmt.Errorf(str+": [%w]", err) }
func Cause(err error) error { return builtin_errors.Unwrap(err) }
func Wrapf(err error, fmtstr string, args ...interface{}) error {
return fmt.Errorf(fmt.Sprintf(fmtstr, args...)+": [%w]", err)
// Method from github.com/unbxd/go-base/v2/errors
func Wrap(err error, message string) error {
if err == nil {
return nil
}

return &withCause{
err, message,
}
}

func Cause(err error) error {
if eu, ok := err.(interface{ Unwrap() []error }); ok {
var eret error

errs := eu.Unwrap()

for _, er := range errs {
c, ok := er.(causer)
if ok {
eret = c.Cause()
break
}
}

if eret == nil {
eret = errs[len(errs)-1]
}

return eret
} else {
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
}
return err
}
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}

return &withCause{
err, fmt.Sprintf(format, args...),
}
}
49 changes: 49 additions & 0 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func TestWrapWithIs(t *testing.T) {
t.Errorf("Wrap() error = %v, wantErr %v", err, tt.want)
}

if Cause(err) != tt.args.err {
t.Errorf("Cause() error = %v, wantErr %v", err, tt.args.err)
}

if e := Unwrap(err); e != tt.args.err {
t.Errorf("Unwrap() error = %v, wantErr %v", err, tt.args.err)
}
},
)
}
Expand All @@ -85,3 +92,45 @@ func TestWrapWithIs(t *testing.T) {
}

}

func TestWith(t *testing.T) {
e1 := New("err 1")
e2 := New("err 2")
e3 := New("err 3")
e4 := New("err 4")
e5 := New("err 5")

type args struct {
err error
errs []error
}
tests := []struct {
name string
args args
wantErr bool
is error
cause error
}{
// TODO: Add test cases.
{"join test", args{e1, []error{e2, e3}}, true, e2, e1},
{"join test 2", args{e1, []error{e4, e5}}, true, e5, e1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := With(tt.args.err, tt.args.errs...)
if (err != nil) != tt.wantErr {
t.Errorf("With() error = %v, wantErr %v", err, tt.wantErr)
}

if !Is(err, tt.is) {
t.Errorf("Is() error = %v, wantErr %v", err, tt.is)
}

ec := Cause(err)
if ec != tt.cause {
t.Errorf("Cause() error = %v, wantErr %v", err, tt.cause)
}

})
}
}
6 changes: 3 additions & 3 deletions examples/http/simple_http/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/unbxd/go-base/exmple/http/simple_http
module github.com/unbxd/go-base/v2/exmple/http/simple_http

go 1.21

toolchain go1.21.1

require github.com/unbxd/go-base v1.2.9
require github.com/unbxd/go-base/v2 v1.2.9

replace github.com/unbxd/go-base => ../../../
replace github.com/unbxd/go-base/v2 => ../../../

require (
github.com/VividCortex/gohistogram v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions examples/http/simple_http/http_ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
net_http "net/http"
"time"

"github.com/unbxd/go-base/log"
"github.com/unbxd/go-base/transport/http"
"github.com/unbxd/go-base/v2/log"
"github.com/unbxd/go-base/v2/transport/http"
)

/*
Expand Down
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/unbxd/go-base
module github.com/unbxd/go-base/v2

go 1.21

Expand Down Expand Up @@ -40,13 +40,10 @@ require (
github.com/klauspost/compress v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/nats-io/nats-server/v2 v2.8.4 // indirect
github.com/nats-io/nkeys v0.4.5 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/stretchr/objx v0.4.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.12.0 // indirect
Expand All @@ -55,8 +52,6 @@ require (
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit be72912

Please sign in to comment.