Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warp and cause interface #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Folders
_obj
_test
.idea

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
43 changes: 40 additions & 3 deletions terror_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"runtime"
"strconv"
"strings"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Error is the 'prototype' of a type of errors.
Expand Down Expand Up @@ -61,9 +64,11 @@ type Error struct {
// and the more detail this field explaining the better,
// even some guess of the cause could be included.
Description string
args []interface{}
file string
line int
// Cause is used to warp some third party error.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

cause error
args []interface{}
file string
line int
}

// Class returns ErrClass
Expand Down Expand Up @@ -291,3 +296,35 @@ func (e *Error) UnmarshalJSON(data []byte) error {
}
return nil
}

func (e *Error) WarpCauseError(err error) *Error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to use a shorter method name Wrap (and there is a typo in your signature).

e.cause = err
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return a new instance of when Wrap a message instead of in-place changing the original instance. The error will be a global instance in many cases.

return e
}

func (e *Error) Cause() error {
return e.cause
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cause error may be another wrapper for the real root cause. Do we need to handle this situation?

}

func (e *Error) FastGenWithCause(args ...interface{}) error {
err := *e
err.message = e.cause.Error()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will panic here is Wrap a nil error

err.args = args
return SuspendStack(&err)
}

func (e *Error) GenWithStackByCause(args ...interface{}) error {
err := *e
err.message = e.cause.Error()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

err.args = args
err.fillLineAndFile(1)
return AddStack(&err)
}

func CauseError(err *Error) zap.Field {
return zap.Field{Key: "error", Type: zapcore.ErrorType, Interface: err.FastGenWithCause()}
}

func DetailError(err *Error) zap.Field {
return zap.Field{Key: "error", Type: zapcore.ErrorType, Interface: err.FastGenByArgs()}
}
9 changes: 9 additions & 0 deletions terror_test/terror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
ClassParser = reg.RegisterErrorClass(11, "parser")
ClassServer = reg.RegisterErrorClass(15, "server")
ClassTable = reg.RegisterErrorClass(19, "table")
ClassMember = reg.RegisterErrorClass(20, "member")
)

const (
Expand Down Expand Up @@ -339,3 +340,11 @@ func (*testTErrorSuite) TestLineAndFile(c *C) {
c.Assert(file2, Equals, f2)
c.Assert(line2, Equals, l2-1)
}

func (*testTErrorSuite) TestWarpAndField(c *C) {
causeErr := errors.New("load from etcd meet error")
ErrGetLeader := ClassMember.DefineError().TextualCode("ErrGetLeader").MessageTemplate("fail to get leader").Build()
errWithWarpedCause := ErrGetLeader.WarpCauseError(causeErr)
c.Assert(errWithWarpedCause.FastGenWithCause().Error(), Equals, "[DB:member:ErrGetLeader] load from etcd meet error")
c.Assert(fmt.Sprintf("%v", errWithWarpedCause.FastGenWithCause()), Equals, "[DB:member:ErrGetLeader] fail to get leader")
}