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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add method to generate error instance
imtbkcat committed Aug 12, 2020
commit 7b34aeb4e57cb3e017604b731916623c8235c709
67 changes: 35 additions & 32 deletions terror_error.go
Original file line number Diff line number Diff line change
@@ -67,12 +67,12 @@ type Error struct {
message string
// The workaround field: how to work around this error.
// It's used to teach the users how to solve the error if occurring in the real environment.
Workaround string
workaround string
// Description is the expanded detail of why this error occurred.
// This could be written by developer at a static env,
// and the more detail this field explaining the better,
// even some guess of the cause could be included.
Description string
description string
// 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{}
@@ -115,34 +115,6 @@ func (e *Error) MessageTemplate() string {
return e.message
}

// NewError creates a standard error object.
func NewError(code int, message string) *Error {
return &Error{
code: ErrCode(code),
message: message,
}
}

// NewErrorWithText creates a standard error object using text error code.
func NewErrorWithText(code string, message string) *Error {
return &Error{
codeText: ErrCodeText(code),
message: message,
}
}

// SetWorkaround sets the workaround for standard error.
func (e *Error) SetWorkaround(workaround string) *Error {
e.Workaround = workaround
return e
}

// SetWorkaround sets the description for standard error.
func (e *Error) SetDescription(description string) *Error {
e.Description = description
return e
}

// SetErrCodeText sets the text error code for standard error.
func (e *Error) SetErrCodeText(codeText string) *Error {
e.codeText = ErrCodeText(codeText)
@@ -277,8 +249,8 @@ type jsonError struct {
func (e *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonError{
Error: e.GetMsg(),
Description: e.Description,
Workaround: e.Workaround,
Description: e.description,
Workaround: e.workaround,
RFCCode: e.RFCCode(),
Line: e.line,
File: e.file,
@@ -337,6 +309,37 @@ func (e *Error) GenWithStackByCause(args ...interface{}) error {
return AddStack(&err)
}

type NormalizeOption func(*Error)

func Description(desc string) NormalizeOption {
return func(e *Error) {
e.description = desc
}
}

func Workaround(wr string) NormalizeOption {
return func(e *Error) {
e.workaround = wr
}
}

func RFCCodeText(codeText string) NormalizeOption {
return func(e *Error) {
e.codeText = ErrCodeText(codeText)
}
}

func Normalize(code int, message string, opts ...NormalizeOption) *Error {
e := &Error{
code: ErrCode(code),
message: message,
}
for _, opt := range opts {
opt(e)
}
return e
}

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