Conversation
terror_error.go
Outdated
| return nil | ||
| } | ||
|
|
||
| func (e *Error) WarpCauseError(err error) *Error { |
There was a problem hiding this comment.
Prefer to use a shorter method name Wrap (and there is a typo in your signature).
terror_error.go
Outdated
| } | ||
|
|
||
| func (e *Error) Cause() error { | ||
| return e.cause |
There was a problem hiding this comment.
The cause error may be another wrapper for the real root cause. Do we need to handle this situation?
| // Class returns ErrClass | ||
| func (e *Error) Class() *ErrClass { | ||
| return e.class | ||
| // Cause is used to warp some third party error. |
| describe = ErrCodeText(strconv.Itoa(int(e.code))) | ||
| } | ||
| return fmt.Sprintf("[%s] %s", e.RFCCode(), e.getMsg()) | ||
| return fmt.Sprintf("[%s] %s", e.RFCCode(), e.GetMsg()) |
There was a problem hiding this comment.
I think we should use the local variable describe as the prefix of the error message.
| } | ||
|
|
||
| func (e *Error) Wrap(err error) *Error { | ||
| e.cause = err |
There was a problem hiding this comment.
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.
| // The error code is a 3-tuple of abbreviated component name, error class and error code, | ||
| // joined by a colon like {Component}:{ErrorClass}:{InnerErrorCode}. | ||
| func (e *Error) RFCCode() RFCErrorCode { | ||
| ec := e.Class() |
There was a problem hiding this comment.
We should return the e.codeText if the e.codeText had been assigned a value.
terror_error.go
Outdated
|
|
||
| // SetWorkaround sets the workaround for standard error. | ||
| func (e *Error) SetWorkaround(workaround string) *Error { | ||
| e.Workaround = workaround |
There was a problem hiding this comment.
Can we allow users to dynamically change the fields?
There was a problem hiding this comment.
This is a decorator function. Since old error in TiDB do not have workaround field. Using this method can add workaround with miniumn change.
There was a problem hiding this comment.
Reference to the demo https://play.golang.org/p/rJBVbbkbXLP, we can minimize the change and make the error immutable.
| return zap.Field{Key: "error", Type: zapcore.ErrorType, Interface: err.FastGenWithCause()} | ||
| } | ||
|
|
||
| // CauseError returns zap.Field contains error. |
There was a problem hiding this comment.
| // CauseError returns zap.Field contains error. | |
| // DetailError returns zap.Field contains error. |
There was a problem hiding this comment.
BTW, I think we can combine these two methods? And Wrap the cause error inside of this function so that we can directly use something like CauseError(err, causeErr).
| func CauseError(err *Error) zap.Field { | ||
| return zap.Field{Key: "error", Type: zapcore.ErrorType, Interface: err.FastGenWithCause()} | ||
| } | ||
|
|
||
| // CauseError returns zap.Field contains error. | ||
| func DetailError(err *Error) zap.Field { | ||
| return zap.Field{Key: "error", Type: zapcore.ErrorType, Interface: err.FastGenByArgs()} | ||
| } |
There was a problem hiding this comment.
According to discussion with @rleungx, we can remove this two functions and use zap.Error directly. And PD will customize a function to adopt their scenarios.
|
|
||
| func (e *Error) FastGenWithCause(args ...interface{}) error { | ||
| err := *e | ||
| err.message = e.cause.Error() |
|
|
||
| func (e *Error) GenWithStackByCause(args ...interface{}) error { | ||
| err := *e | ||
| err.message = e.cause.Error() |
WarpandCauseinterface to save cause error.FastGenWithCauseto generate error with error code and cause error