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

Redact arguments for Error #27

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Changes from all 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
31 changes: 30 additions & 1 deletion terror_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"strings"
)

// RedactLogEnabled defines whether the arguments of Error need to be redacted.
var RedactLogEnabled bool = false

// ErrCode represents a specific error type in a error class.
// Same error code can be used in different error classes.
type ErrCode int
Expand Down Expand Up @@ -109,11 +112,16 @@ type Error struct {
// 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
// Description is the expanded detail of why this error occurred.
// 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
// redactArgsPos defines the positions of arguments in message that need to be redacted.
// And it is controlled by the global var RedactLogEnabled.
// For example, an original error is `Duplicate entry 'PRIMARY' for key 'key'`,
// when RedactLogEnabled is ON and redactArgsPos is [0, 1], the error is `Duplicate entry '?' for key '?'`.
redactArgsPos []int
// Cause is used to warp some third party error.
cause error
args []interface{}
Expand Down Expand Up @@ -189,6 +197,7 @@ func (e *Error) fillLineAndFile(skip int) {

// GenWithStack generates a new *Error with the same class and code, and a new formatted message.
func (e *Error) GenWithStack(format string, args ...interface{}) error {
// TODO: RedactErrorArg
err := *e
err.message = format
err.args = args
Expand All @@ -198,6 +207,7 @@ func (e *Error) GenWithStack(format string, args ...interface{}) error {

// GenWithStackByArgs generates a new *Error with the same class and code, and new arguments.
func (e *Error) GenWithStackByArgs(args ...interface{}) error {
RedactErrorArg(args, e.redactArgsPos)
err := *e
err.args = args
err.fillLineAndFile(1)
Expand All @@ -207,6 +217,7 @@ func (e *Error) GenWithStackByArgs(args ...interface{}) error {
// FastGen generates a new *Error with the same class and code, and a new formatted message.
// This will not call runtime.Caller to get file and line.
func (e *Error) FastGen(format string, args ...interface{}) error {
// TODO: RedactErrorArg
err := *e
err.message = format
err.args = args
Expand All @@ -216,6 +227,7 @@ func (e *Error) FastGen(format string, args ...interface{}) error {
// FastGen generates a new *Error with the same class and code, and a new arguments.
// This will not call runtime.Caller to get file and line.
func (e *Error) FastGenByArgs(args ...interface{}) error {
RedactErrorArg(args, e.redactArgsPos)
err := *e
err.args = args
return SuspendStack(&err)
Expand Down Expand Up @@ -243,6 +255,17 @@ func (e *Error) NotEqual(err error) bool {
return !e.Equal(err)
}

// RedactErrorArg redacts the args by position if RedactLogEnabled is enabled.
func RedactErrorArg(args []interface{}, position []int) {
if RedactLogEnabled {
for _, pos := range position {
if len(args) > pos {
args[pos] = "?"
}
}
}
}

// ErrorEqual returns a boolean indicating whether err1 is equal to err2.
func ErrorEqual(err1, err2 error) bool {
e1 := Cause(err1)
Expand Down Expand Up @@ -364,6 +387,12 @@ func Workaround(wr string) NormalizeOption {
}
}

func RedactArgs(pos []int) NormalizeOption {
return func(e *Error) {
e.redactArgsPos = pos
}
}

// RFCCodeText returns a NormalizeOption to set RFC error code.
func RFCCodeText(codeText string) NormalizeOption {
return func(e *Error) {
Expand Down