github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/redact/redact.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package redact
    15  
    16  import (
    17  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    18  	"github.com/whtcorpsinc/milevadb/config"
    19  )
    20  
    21  // TError is a alias, use to avoid `Error` method name in conflict with field name.
    22  type TError = terror.Error
    23  
    24  type redactError struct {
    25  	*TError
    26  	redactPositions []int
    27  }
    28  
    29  // GenWithStackByArgs generates a new *Error with the same class and code, and new arguments.
    30  func (e *redactError) GenWithStackByArgs(args ...interface{}) error {
    31  	redactErrorArg(args, e.redactPositions)
    32  	return e.TError.GenWithStackByArgs(args...)
    33  }
    34  
    35  // FastGen generates a new *Error with the same class and code, and a new arguments.
    36  func (e *redactError) FastGenByArgs(args ...interface{}) error {
    37  	redactErrorArg(args, e.redactPositions)
    38  	return e.TError.GenWithStackByArgs(args...)
    39  }
    40  
    41  // Equal checks if err is equal to e.
    42  func (e *redactError) Equal(err error) bool {
    43  	if redactErr, ok := err.(*redactError); ok {
    44  		return e.TError.Equal(redactErr.TError)
    45  	}
    46  	return e.TError.Equal(err)
    47  }
    48  
    49  // Cause implement the Cause interface.
    50  func (e *redactError) Cause() error {
    51  	return e.TError
    52  }
    53  
    54  func redactErrorArg(args []interface{}, position []int) {
    55  	if config.RedactLogEnabled() {
    56  		for _, pos := range position {
    57  			if len(args) > pos {
    58  				args[pos] = "?"
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  // NewRedactError returns a new redact error.
    65  func NewRedactError(err *terror.Error, redactPositions ...int) *redactError {
    66  	return &redactError{err, redactPositions}
    67  }