1 Star 0 Fork 0

sy_183/go-common

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
errors.go 1.17 KB
Copy Edit Raw Blame History
sy_183 authored 2023-07-20 13:21 +08:00 . 1. 增加了 lifecycle 泛型属性的函数
package errors
import (
"fmt"
"strings"
)
type ErrorGroup interface {
Errors() []error
}
type Errors []error
func (es Errors) Append(err error) Errors {
if err != nil {
return append(es, err)
}
return es
}
func (es Errors) Errors() []error {
return es
}
func (es Errors) Error() string {
if len(es) == 0 {
return "<nil>"
}
ess := make([]string, len(es))
for i, err := range es {
s := fmt.Sprintf("%q", err)
if i == 0 {
s = "[" + s
}
if i == len(es)-1 {
s += "]"
}
ess[i] = s
}
return strings.Join(ess, ", ")
}
func (es Errors) ToError() error {
if len(es) == 0 {
return nil
}
return es
}
func MakeErrors(es ...error) error {
var err error
for _, e := range es {
err = Append(err, e)
}
return err
}
func Append(left error, right error) error {
switch {
case left == nil:
return right
case right == nil:
return left
}
if r, ok := right.(Errors); ok {
if l, ok := left.(Errors); ok {
return append(l, r...)
}
return append(Errors{left}, r...)
} else if l, ok := left.(Errors); ok {
return append(l, right)
}
return Errors{left, right}
}
type StringError string
func (s StringError) Error() string {
return string(s)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sy_183/go-common.git
git@gitee.com:sy_183/go-common.git
sy_183
go-common
go-common
v1.0.4

Search