github.com/segmentio/encoding@v0.4.0/thrift/error.go (about)

     1  package thrift
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  )
     9  
    10  type MissingField struct {
    11  	Field Field
    12  }
    13  
    14  func (e *MissingField) Error() string {
    15  	return fmt.Sprintf("missing required field: %s", e.Field)
    16  }
    17  
    18  type TypeMismatch struct {
    19  	Expect Type
    20  	Found  Type
    21  	item   string
    22  }
    23  
    24  func (e *TypeMismatch) Error() string {
    25  	return fmt.Sprintf("%s type mismatch: expected %s but found %s", e.item, e.Expect, e.Found)
    26  }
    27  
    28  type decodeError struct {
    29  	base error
    30  	path []error
    31  }
    32  
    33  func (e *decodeError) Error() string {
    34  	s := strings.Builder{}
    35  	s.Grow(256)
    36  	s.WriteString("decoding thrift payload: ")
    37  
    38  	if len(e.path) != 0 {
    39  		n := len(e.path) - 1
    40  		for i := n; i >= 0; i-- {
    41  			if i < n {
    42  				s.WriteString(" → ")
    43  			}
    44  			s.WriteString(e.path[i].Error())
    45  		}
    46  		s.WriteString(": ")
    47  	}
    48  
    49  	s.WriteString(e.base.Error())
    50  	return s.String()
    51  }
    52  
    53  func (e *decodeError) Unwrap() error { return e.base }
    54  
    55  func with(base, elem error) error {
    56  	if errors.Is(base, io.EOF) {
    57  		return base
    58  	}
    59  	e, _ := base.(*decodeError)
    60  	if e == nil {
    61  		e = &decodeError{base: base}
    62  	}
    63  	e.path = append(e.path, elem)
    64  	return e
    65  }
    66  
    67  type decodeErrorField struct {
    68  	cause Field
    69  }
    70  
    71  func (d *decodeErrorField) Error() string {
    72  	return d.cause.String()
    73  }
    74  
    75  type decodeErrorList struct {
    76  	cause List
    77  	index int
    78  }
    79  
    80  func (d *decodeErrorList) Error() string {
    81  	return fmt.Sprintf("%d/%d:%s", d.index, d.cause.Size, d.cause)
    82  }
    83  
    84  type decodeErrorSet struct {
    85  	cause Set
    86  	index int
    87  }
    88  
    89  func (d *decodeErrorSet) Error() string {
    90  	return fmt.Sprintf("%d/%d:%s", d.index, d.cause.Size, d.cause)
    91  }
    92  
    93  type decodeErrorMap struct {
    94  	cause Map
    95  	index int
    96  }
    97  
    98  func (d *decodeErrorMap) Error() string {
    99  	return fmt.Sprintf("%d/%d:%s", d.index, d.cause.Size, d.cause)
   100  }
   101  
   102  func dontExpectEOF(err error) error {
   103  	switch err {
   104  	case nil:
   105  		return nil
   106  	case io.EOF:
   107  		return io.ErrUnexpectedEOF
   108  	default:
   109  		return err
   110  	}
   111  }