Skip to content

Commit

Permalink
feat: errorx - add some feat error types to errorx
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 19, 2022
1 parent f335749 commit b177c93
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions errorx/reply.go → errorx/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errorx
import (
"fmt"
"strconv"
"strings"
)

// ErrorCoder interface
Expand Down Expand Up @@ -70,3 +71,57 @@ func (e *errorR) String() string {
func (e *errorR) GoString() string {
return e.String()
}

// ErrMap type
type ErrMap map[string]error

// Error string
func (e ErrMap) Error() string {
var sb strings.Builder
for name, err := range e {
sb.WriteString(name)
sb.WriteByte(':')
sb.WriteString(err.Error())
sb.WriteByte('\n')
}
return sb.String()
}

// IsEmpty error
func (e ErrMap) IsEmpty() bool {
return len(e) == 0
}

// One error
func (e ErrMap) One() error {
for _, err := range e {
return err
}
return nil
}

// ErrList type
type ErrList []error

// Error string
func (el ErrList) Error() string {
var sb strings.Builder
for _, err := range el {
sb.WriteString(err.Error())
sb.WriteByte('\n')
}
return sb.String()
}

// IsEmpty error
func (el ErrList) IsEmpty() bool {
return len(el) == 0
}

// First error
func (el ErrList) First() error {
if len(el) > 0 {
return el[0]
}
return nil
}
File renamed without changes.

0 comments on commit b177c93

Please sign in to comment.