github.com/lheiskan/zebrapack@v4.1.1-0.20181107023619-e955d028f9bf+incompatible/msgp/errors.go (about)

     1  package msgp
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  var (
     9  	// ErrShortBytes is returned when the
    10  	// slice being decoded is too short to
    11  	// contain the contents of the message
    12  	ErrShortBytes error = errShort{}
    13  
    14  	// this error is only returned
    15  	// if we reach code that should
    16  	// be unreachable
    17  	fatal error = errFatal{}
    18  )
    19  
    20  // Error is the interface satisfied
    21  // by all of the errors that originate
    22  // from this package.
    23  type Error interface {
    24  	error
    25  
    26  	// Resumable returns whether
    27  	// or not the error means that
    28  	// the stream of data is malformed
    29  	// and  the information is unrecoverable.
    30  	Resumable() bool
    31  }
    32  
    33  type errShort struct{}
    34  
    35  func (e errShort) Error() string   { return "msgp: too few bytes left to read object" }
    36  func (e errShort) Resumable() bool { return false }
    37  
    38  type errFatal struct{}
    39  
    40  func (f errFatal) Error() string   { return "msgp: fatal decoding error (unreachable code)" }
    41  func (f errFatal) Resumable() bool { return false }
    42  
    43  // ArrayError is an error returned
    44  // when decoding a fix-sized array
    45  // of the wrong size
    46  type ArrayError struct {
    47  	Wanted uint32
    48  	Got    uint32
    49  }
    50  
    51  // Error implements the error interface
    52  func (a ArrayError) Error() string {
    53  	return fmt.Sprintf("msgp: wanted array of size %d; got %d", a.Wanted, a.Got)
    54  }
    55  
    56  // Resumable is always 'true' for ArrayErrors
    57  func (a ArrayError) Resumable() bool { return true }
    58  
    59  // IntOverflow is returned when a call
    60  // would downcast an integer to a type
    61  // with too few bits to hold its value.
    62  type IntOverflow struct {
    63  	Value         int64 // the value of the integer
    64  	FailedBitsize int   // the bit size that the int64 could not fit into
    65  }
    66  
    67  // Error implements the error interface
    68  func (i IntOverflow) Error() string {
    69  	return fmt.Sprintf("msgp: %d overflows int%d", i.Value, i.FailedBitsize)
    70  }
    71  
    72  // Resumable is always 'true' for overflows
    73  func (i IntOverflow) Resumable() bool { return true }
    74  
    75  // UintOverflow is returned when a call
    76  // would downcast an unsigned integer to a type
    77  // with too few bits to hold its value
    78  type UintOverflow struct {
    79  	Value         uint64 // value of the uint
    80  	FailedBitsize int    // the bit size that couldn't fit the value
    81  }
    82  
    83  // Error implements the error interface
    84  func (u UintOverflow) Error() string {
    85  	return fmt.Sprintf("msgp: %d overflows uint%d", u.Value, u.FailedBitsize)
    86  }
    87  
    88  // Resumable is always 'true' for overflows
    89  func (u UintOverflow) Resumable() bool { return true }
    90  
    91  // A TypeError is returned when a particular
    92  // decoding method is unsuitable for decoding
    93  // a particular MessagePack value.
    94  type TypeError struct {
    95  	Method  Type // Type expected by method
    96  	Encoded Type // Type actually encoded
    97  }
    98  
    99  // Error implements the error interface
   100  func (t TypeError) Error() string {
   101  	return fmt.Sprintf("msgp: attempted to decode type %q with method for %q", t.Encoded, t.Method)
   102  }
   103  
   104  // Resumable returns 'true' for TypeErrors
   105  func (t TypeError) Resumable() bool { return true }
   106  
   107  // returns either InvalidPrefixError or
   108  // TypeError depending on whether or not
   109  // the prefix is recognized
   110  func badPrefix(want Type, lead byte) error {
   111  	t := sizes[lead].typ
   112  	if t == InvalidType {
   113  		return InvalidPrefixError(lead)
   114  	}
   115  	return TypeError{Method: want, Encoded: t}
   116  }
   117  
   118  // InvalidPrefixError is returned when a bad encoding
   119  // uses a prefix that is not recognized in the MessagePack standard.
   120  // This kind of error is unrecoverable.
   121  type InvalidPrefixError byte
   122  
   123  // Error implements the error interface
   124  func (i InvalidPrefixError) Error() string {
   125  	return fmt.Sprintf("msgp: unrecognized type prefix 0x%x", byte(i))
   126  }
   127  
   128  // Resumable returns 'false' for InvalidPrefixErrors
   129  func (i InvalidPrefixError) Resumable() bool { return false }
   130  
   131  // ErrUnsupportedType is returned
   132  // when a bad argument is supplied
   133  // to a function that takes `interface{}`.
   134  type ErrUnsupportedType struct {
   135  	T reflect.Type
   136  }
   137  
   138  // Error implements error
   139  func (e *ErrUnsupportedType) Error() string { return fmt.Sprintf("msgp: type %q not supported", e.T) }
   140  
   141  // Resumable returns 'true' for ErrUnsupportedType
   142  func (e *ErrUnsupportedType) Resumable() bool { return true }