gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/errors/invalid-argument.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  func AddParentArgument(argument, parent string) string {
     9  	if argument == "" {
    10  		return parent
    11  	} else {
    12  		return parent + "." + argument
    13  	}
    14  }
    15  
    16  func ReplaceParentArgument(argument, parent string) string {
    17  	if argument == "" {
    18  		return parent
    19  	} else if i := strings.IndexByte(argument, '.'); i >= 0 {
    20  		return parent + argument[i:]
    21  	} else {
    22  		return parent + "." + argument
    23  	}
    24  }
    25  
    26  type InvalidArgument struct {
    27  	Argument string
    28  	Err      error
    29  }
    30  
    31  func NewInvalidArgument(argument string, err error) *InvalidArgument {
    32  	return &InvalidArgument{Argument: argument, Err: err}
    33  }
    34  
    35  func (e *InvalidArgument) Error() string {
    36  	if e == nil {
    37  		return "<nil>"
    38  	} else if e.Argument == "" {
    39  		return "参数解析错误:" + e.Err.Error()
    40  	} else {
    41  		return fmt.Sprintf("参数解析错误(%s):%s", e.Argument, e.Err.Error())
    42  	}
    43  }
    44  
    45  func (e *InvalidArgument) AddParentArgument(parent string) {
    46  	e.Argument = AddParentArgument(e.Argument, parent)
    47  }
    48  
    49  func (e *InvalidArgument) ReplaceParentArgument(parent string) {
    50  	e.Argument = ReplaceParentArgument(e.Argument, parent)
    51  }