-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
2493a22
b667838
b17b426
2548b76
a28915e
2d79068
7b34aeb
57ec461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
# Folders | ||
_obj | ||
_test | ||
.idea | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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. | ||
cause error | ||
args []interface{} | ||
file string | ||
line int | ||
} | ||
|
||
// Class returns ErrClass | ||
|
@@ -291,3 +296,35 @@ func (e *Error) UnmarshalJSON(data []byte) error { | |
} | ||
return nil | ||
} | ||
|
||
func (e *Error) WarpCauseError(err error) *Error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer to use a shorter method name |
||
e.cause = err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should return a new instance of when |
||
return e | ||
} | ||
|
||
func (e *Error) Cause() error { | ||
return e.cause | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will panic here is |
||
err.args = args | ||
return SuspendStack(&err) | ||
} | ||
|
||
func (e *Error) GenWithStackByCause(args ...interface{}) error { | ||
err := *e | ||
err.message = e.cause.Error() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo