]> 127.0.0.1 Git - part/.git/commitdiff
1 (#13) v0.28.20250120183746
authorqydysky <qydysky@foxmail.com>
Mon, 20 Jan 2025 18:32:13 +0000 (02:32 +0800)
committerGitHub <noreply@github.com>
Mon, 20 Jan 2025 18:32:13 +0000 (02:32 +0800)
errors/errors.go
errors/errors_test.go
reqf/Reqf.go

index 63554c336ff9bad255258bf6025162c80d8789e4..e83fdf41c3688f642568e1a3bae3141dd45f093a 100644 (file)
@@ -2,6 +2,7 @@ package errors
 
 import (
        "errors"
+       "fmt"
 )
 
 type Action string
@@ -10,16 +11,36 @@ func (t Action) Append(child string) Action {
        return Action(string(t) + child)
 }
 
+func (t Action) New(reason ...string) (e Error) {
+       e = Error{
+               action: t,
+               Reason: string(t),
+       }
+       if len(reason) > 0 {
+               e.Reason = reason[0]
+       }
+       return
+}
+
 type Error struct {
        son    error
        Reason string
        action Action
 }
 
+func (t Error) Is(e error) bool {
+       fmt.Println(t, e)
+       return t.Error() == e.Error()
+}
+
 func (t Error) Error() string {
        return t.Reason
 }
 
+func (t Error) Unwrap() error {
+       return nil
+}
+
 func (t Error) WithReason(reason string) Error {
        t.Reason = reason
        return t
@@ -29,23 +50,16 @@ func Catch(e error, action Action) bool {
        if v, ok := e.(Error); ok {
                if v.action == action {
                        return true
-               } else if v.son != nil {
-                       return Catch((v.son).(Error), action)
                }
        }
-       return false
-}
-
-// Grow will append e to fe
-func Append(fe Error, e error) Error {
-       if v, ok := e.(Error); ok {
-               fe.son = v
-       } else {
-               fe.son = Error{
-                       Reason: e.Error(),
+       for _, err := range Unwrap(e) {
+               if v, ok := err.(Error); ok {
+                       if v.action == action {
+                               return true
+                       }
                }
        }
-       return fe
+       return false
 }
 
 func New(reason string, action ...Action) (e Error) {
@@ -65,11 +79,10 @@ func Join(e ...error) error {
 
        var errs []error
        for _, v := range e {
-               if e, ok := v.(interface {
-                       Unwrap() []error
-               }); ok {
-                       errs = append(errs, e.Unwrap()...)
-               } else {
+               switch x := v.(type) {
+               case interface{ Unwrap() []error }:
+                       errs = append(errs, x.Unwrap()...)
+               default:
                        errs = append(errs, v)
                }
        }
@@ -82,10 +95,12 @@ func Unwrap(e error) []error {
                return []error{}
        }
 
-       if e, ok := e.(interface {
-               Unwrap() []error
-       }); ok {
-               return e.Unwrap()
+       switch x := e.(type) {
+       case interface{ Unwrap() error }:
+               return []error{x.Unwrap()}
+       case interface{ Unwrap() []error }:
+               return x.Unwrap()
+       default:
        }
 
        return []error{errors.Unwrap(e)}
@@ -123,7 +138,7 @@ var (
                return e.Error() + "\n"
        }
        ErrActionSimplifyFunc ErrFormat = func(e error) string {
-               if err, ok := e.(Error); ok {
+               if err, ok := e.(Error); ok && string(err.action) != err.Reason {
                        return string(err.action) + ":" + e.Error() + "\n"
                } else {
                        return e.Error() + "\n"
@@ -133,7 +148,7 @@ var (
                return "> " + e.Error() + " "
        }
        ErrActionInLineFunc ErrFormat = func(e error) string {
-               if err, ok := e.(Error); ok {
+               if err, ok := e.(Error); ok && string(err.action) != err.Reason {
                        return "> " + string(err.action) + ":" + e.Error() + " "
                } else {
                        return "> " + e.Error() + " "
index 9836814bda87566e42f4544faf7a9c2509c1e9c1..ec0a4dda7d6b1ff3b668a8c02a0d275cf7d54a89 100644 (file)
@@ -3,6 +3,7 @@ package errors
 import (
        "errors"
        "io"
+       "os"
        "testing"
 )
 
@@ -23,7 +24,7 @@ func TestXxx(t *testing.T) {
                t.Fail()
        }
 
-       err = Append(New("r1", a11), err)
+       err = Join(New("r1", a11), err)
 
        if !Catch(err, a11) {
                t.Fail()
@@ -35,7 +36,7 @@ func TestXxx(t *testing.T) {
 }
 
 func TestXxx2(t *testing.T) {
-       err := Append(New("r1", a1), io.EOF)
+       err := Join(New("r1", a1), io.EOF)
        if !Catch(err, a1) {
                t.Fatal()
        }
@@ -82,3 +83,27 @@ func Test3(t *testing.T) {
                t.Fatal()
        }
 }
+
+func Test4(t *testing.T) {
+       var Action1 Action = `Action1`
+       e := Join(os.ErrClosed, Action1.New(), os.ErrDeadlineExceeded)
+       for _, v := range Unwrap(e) {
+               t.Log(v)
+       }
+       if !Catch(e, Action1) {
+               t.Fatal()
+       }
+       if Catch(e, a1) {
+               t.Fatal()
+       }
+       if !errors.Is(e, Action1.New()) {
+               t.Fatal()
+       }
+       if !errors.Is(e, os.ErrClosed) {
+               t.Fatal()
+       }
+       if errors.Is(e, io.EOF) {
+               t.Fatal()
+       }
+       t.Log(ErrorFormat(e, ErrActionInLineFunc))
+}
index fbe85f3f5165d3b40f06179ca6a1781c71a9a874..5411b3388e4ed3cc1bbe057ecfaca5c1de29898e 100644 (file)
@@ -20,6 +20,7 @@ import (
        gzip "compress/gzip"
 
        br "github.com/qydysky/brotli"
+       pe "github.com/qydysky/part/errors"
        pio "github.com/qydysky/part/io"
        s "github.com/qydysky/part/strings"
        // "encoding/binary"
@@ -61,15 +62,15 @@ const (
 )
 
 var (
-       ErrEmptyUrl         = errors.New("ErrEmptyUrl")
-       ErrCantRetry        = errors.New("ErrCantRetry")
-       ErrNewRequest       = errors.New("ErrNewRequest")
-       ErrClientDo         = errors.New("ErrClientDo")
-       ErrResponFileCreate = errors.New("ErrResponFileCreate")
-       ErrWriteRes         = errors.New("ErrWriteRes")
-       ErrReadRes          = errors.New("ErrReadRes")
-       ErrPostStrOrRawPipe = errors.New("ErrPostStrOrRawPipe")
-       ErrNoDate           = errors.New("ErrNoDate")
+       ErrEmptyUrl         = pe.Action("ErrEmptyUrl")
+       ErrCantRetry        = pe.Action("ErrCantRetry")
+       ErrNewRequest       = pe.Action("ErrNewRequest")
+       ErrClientDo         = pe.Action("ErrClientDo")
+       ErrResponFileCreate = pe.Action("ErrResponFileCreate")
+       ErrWriteRes         = pe.Action("ErrWriteRes")
+       ErrReadRes          = pe.Action("ErrReadRes")
+       ErrPostStrOrRawPipe = pe.Action("ErrPostStrOrRawPipe")
+       ErrNoDate           = pe.Action("ErrNoDate")
 )
 
 type Req struct {
@@ -161,15 +162,15 @@ func (t *Req) reqf(ctx context.Context, val Rval) (err error) {
        }
 
        if val.Url == "" {
-               return ErrEmptyUrl
+               return ErrEmptyUrl.New()
        }
 
        var body io.Reader
        if len(val.PostStr) > 0 && val.RawPipe != nil {
-               return ErrPostStrOrRawPipe
+               return ErrPostStrOrRawPipe.New()
        }
        if val.Retry != 0 && val.RawPipe != nil {
-               return ErrCantRetry
+               return ErrCantRetry.New()
        }
        if val.RawPipe != nil {
                body = val.RawPipe
@@ -184,7 +185,7 @@ func (t *Req) reqf(ctx context.Context, val Rval) (err error) {
 
        req, e := http.NewRequestWithContext(ctx, val.Method, val.Url, body)
        if e != nil {
-               return errors.Join(ErrNewRequest, e)
+               return pe.Join(ErrNewRequest.New(), e)
        }
 
        for _, v := range val.Cookies {
@@ -214,7 +215,7 @@ func (t *Req) reqf(ctx context.Context, val Rval) (err error) {
        resp, e := client.Do(req)
 
        if e != nil {
-               return errors.Join(ErrClientDo, e)
+               return pe.Join(ErrClientDo.New(), e)
        }
 
        if v, ok := Header["Connection"]; ok && strings.ToLower(v) != "keep-alive" {
@@ -236,7 +237,7 @@ func (t *Req) reqf(ctx context.Context, val Rval) (err error) {
                t.responFile, e = os.Create(val.SaveToPath)
                if e != nil {
                        t.responFile.Close()
-                       return errors.Join(err, ErrResponFileCreate, e)
+                       return pe.Join(err, ErrResponFileCreate.New(), e)
                }
                ws = append(ws, t.responFile)
        }
@@ -280,10 +281,10 @@ func (t *Req) reqf(ctx context.Context, val Rval) (err error) {
                t.copyResBuf[:],
                time.Duration(int(time.Millisecond)*writeLoopTO), io.MultiWriter(ws...),
                resReadCloser,
-               func(s string) { errChan <- errors.New(s) },
+               func(s string) { errChan <- pe.New(s) },
        )
        for len(errChan) > 0 {
-               err = errors.Join(err, <-errChan)
+               err = pe.Join(err, <-errChan)
        }
 
        if t.responBuf != nil {
@@ -436,6 +437,6 @@ func ResDate(res *http.Response) (time.Time, error) {
        if date := res.Header.Get("date"); date != `` {
                return time.Parse(time.RFC1123, date)
        } else {
-               return time.Time{}, ErrNoDate
+               return time.Time{}, ErrNoDate.New()
        }
 }