Skip to content

Commit

Permalink
Merge pull request #1 from muonsoft/joining
Browse files Browse the repository at this point in the history
Joining
  • Loading branch information
strider2038 authored Aug 9, 2023
2 parents 21bcd07 + 7634999 commit e6d54eb
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.20
id: go

- name: Checkout code
Expand All @@ -25,7 +25,7 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.46
version: v1.52

- name: Run tests
run: go test -v $(go list ./... | grep -v vendor)
11 changes: 6 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ linters:
- bidichk
- bodyclose
- contextcheck
- cyclop
- deadcode
- depguard
- dogsled
- dupl
- durationcheck
- exportloopref
- forbidigo
- funlen
- gci
- gocognit
- goconst
- gocritic
Expand Down Expand Up @@ -46,7 +43,6 @@ linters:
- predeclared
- promlinter
- revive
- structcheck
- stylecheck
- tenv
- testpackage
Expand All @@ -56,7 +52,6 @@ linters:
- unconvert
- unparam
- unused
- varcheck
- whitespace

issues:
Expand Down Expand Up @@ -84,3 +79,9 @@ issues:
- errcheck
- goconst
- gocritic

linters-settings:
revive:
rules:
- name: var-naming
disabled: true
64 changes: 47 additions & 17 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,25 @@ func As[T any](err error) (T, bool) {
return t, true
}
}
err = Unwrap(err)
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
if err == nil {
var z T
return z, false
}
case interface{ Unwrap() []error }:
for _, err := range x.Unwrap() {
if t, ok := As[T](err); ok {
return t, ok
}
}
var z T
return z, false
default:
var z T
return z, false
}
}

var z T
Expand Down Expand Up @@ -106,8 +124,8 @@ func Errorf(message string, argsAndOptions ...interface{}) error {
opts := newOptions(options...)
err := fmt.Errorf(message, args...)

argError := getArgError(message, args)
if isWrapper(argError) {
argErrors := getArgErrors(message, args)
if len(argErrors) == 1 && isWrapper(argErrors[0]) {
return &wrapped{wrapped: err, fields: opts.fields}
}

Expand All @@ -126,12 +144,16 @@ func Wrap(err error, options ...Option) error {
if err == nil {
return nil
}
opts := newOptions(options...)

if isWrapper(err) {
return &wrapped{wrapped: err, fields: opts.fields}
if len(options) == 0 {
return err
}

return &wrapped{wrapped: err, fields: newOptions(options...).fields}
}

opts := newOptions(options...)

return &stacked{
wrapped: &wrapped{wrapped: err, fields: opts.fields},
stack: newStack(opts.skipCallers),
Expand Down Expand Up @@ -230,7 +252,13 @@ func (e *stacked) Format(s fmt.State, verb rune) {
func (e *stacked) MarshalJSON() ([]byte, error) {
data := mapWriter{"error": e.Error()}
data.SetStackTrace(e.StackTrace())
e.LogFields(data)

var err error
for err = e; err != nil; err = Unwrap(err) {
if loggable, ok := err.(LoggableError); ok {
loggable.LogFields(data)
}
}

return json.Marshal(data)
}
Expand All @@ -254,28 +282,30 @@ func splitArgsAndOptions(argsAndOptions []interface{}) ([]interface{}, []Option)
return args, options
}

func getArgError(message string, args []interface{}) error {
index := getErrorIndex(message)
func getArgErrors(message string, args []interface{}) []error {
indices := getErrorIndices(message)
errs := make([]error, 0, len(indices))

if index >= 0 && index < len(args) {
if err, ok := args[index].(error); ok {
return err
for _, i := range indices {
if err, ok := args[i].(error); ok {
errs = append(errs, err)
}
}

return nil
return errs
}

func getErrorIndex(message string) int {
i := -1
func getErrorIndices(message string) []int {
indices := make([]int, 0, 1)
isFormat := false

i := -1
for _, s := range message {
if isFormat {
if s != '%' {
i++
if s == 'w' {
return i
indices = append(indices, i)
}
}
isFormat = false
Expand All @@ -284,7 +314,7 @@ func getErrorIndex(message string) int {
}
}

return -1
return indices
}

type mapWriter map[string]interface{}
Expand Down
Loading

0 comments on commit e6d54eb

Please sign in to comment.