github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/errors/errors.go (about)

     1  package errors
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/machinefi/w3bstream/pkg/depends/x/reflectx"
    11  )
    12  
    13  type SyntaxError struct{ msg string }
    14  
    15  func (e *SyntaxError) Error() string { return e.msg }
    16  
    17  func NewSyntaxError(format string, args ...interface{}) *SyntaxError {
    18  	return &SyntaxError{msg: "invalid syntax: " + fmt.Sprintf(format, args...)}
    19  }
    20  
    21  type UnsupportedTypeError struct{ msg string }
    22  
    23  func (e *UnsupportedTypeError) Error() string { return e.msg }
    24  
    25  func NewUnsupportedTypeError(typ string, rule string, msgs ...string) *UnsupportedTypeError {
    26  	b := &strings.Builder{}
    27  	b.WriteString("unsupported type: rule[" + rule + "] type[" + typ)
    28  	for i, msg := range msgs {
    29  		if i == 0 {
    30  			b.WriteString(": ")
    31  		} else {
    32  			b.WriteString("; ")
    33  		}
    34  		b.WriteString(msg)
    35  	}
    36  	b.WriteString("]")
    37  	return &UnsupportedTypeError{msg: b.String()}
    38  }
    39  
    40  type MissingRequiredFieldError struct{}
    41  
    42  func (MissingRequiredFieldError) Error() string { return "missing required field" }
    43  
    44  type NotMatchError struct {
    45  	Target  string
    46  	Current interface{}
    47  	Pattern *regexp.Regexp
    48  }
    49  
    50  func (err *NotMatchError) Error() string {
    51  	return fmt.Sprintf("%s %s not match %v", err.Target, err.Pattern, err.Current)
    52  }
    53  
    54  type MultipleOfError struct {
    55  	Target     string
    56  	Current    interface{}
    57  	MultipleOf interface{}
    58  }
    59  
    60  func (e *MultipleOfError) Error() string {
    61  	buf := bytes.NewBuffer(nil)
    62  	buf.WriteString(e.Target)
    63  	buf.WriteString(fmt.Sprintf(" should be multiple of %v", e.MultipleOf))
    64  	buf.WriteString(fmt.Sprintf(", but got invalid value %v", e.Current))
    65  	return buf.String()
    66  }
    67  
    68  type NotInEnumError struct {
    69  	Target  string
    70  	Current interface{}
    71  	Enums   []interface{}
    72  }
    73  
    74  func (e *NotInEnumError) Error() string {
    75  	buf := bytes.NewBuffer(nil)
    76  	buf.WriteString(e.Target)
    77  	buf.WriteString(" should be one of ")
    78  
    79  	for i, v := range e.Enums {
    80  		if i > 0 {
    81  			buf.WriteString(", ")
    82  		}
    83  		buf.WriteString(fmt.Sprintf("%v", v))
    84  	}
    85  
    86  	buf.WriteString(fmt.Sprintf(", but got invalid value %v", e.Current))
    87  
    88  	return buf.String()
    89  }
    90  
    91  type OutOfRangeError struct {
    92  	Target           string
    93  	Current          interface{}
    94  	Minimum          interface{}
    95  	Maximum          interface{}
    96  	ExclusiveMaximum bool
    97  	ExclusiveMinimum bool
    98  }
    99  
   100  func (e *OutOfRangeError) Error() string {
   101  	buf := bytes.NewBuffer(nil)
   102  	buf.WriteString(e.Target)
   103  	buf.WriteString(" should be")
   104  
   105  	if e.Minimum != nil {
   106  		buf.WriteString(" larger")
   107  		if e.ExclusiveMinimum {
   108  			buf.WriteString(" or equal")
   109  		}
   110  
   111  		buf.WriteString(fmt.Sprintf(" than %v",
   112  			reflectx.Indirect(reflect.ValueOf(e.Minimum)).Interface()))
   113  	}
   114  
   115  	if e.Maximum != nil {
   116  		if e.Minimum != nil {
   117  			buf.WriteString(" and")
   118  		}
   119  
   120  		buf.WriteString(" less")
   121  		if e.ExclusiveMaximum {
   122  			buf.WriteString(" or equal")
   123  		}
   124  
   125  		buf.WriteString(fmt.Sprintf(" than %v",
   126  			reflectx.Indirect(reflect.ValueOf(e.Maximum)).Interface()))
   127  	}
   128  
   129  	buf.WriteString(fmt.Sprintf(", but got invalid value %v", e.Current))
   130  
   131  	return buf.String()
   132  }