errors.go raw

   1  package document
   2  
   3  import (
   4  	"fmt"
   5  	"reflect"
   6  )
   7  
   8  // UnmarshalTypeError is an error type representing an error
   9  // unmarshaling a Smithy document to a Go value type. This is different
  10  // from UnmarshalError in that it does not wrap an underlying error type.
  11  type UnmarshalTypeError struct {
  12  	Value string
  13  	Type  reflect.Type
  14  }
  15  
  16  // Error returns the string representation of the error.
  17  // Satisfying the error interface.
  18  func (e *UnmarshalTypeError) Error() string {
  19  	return fmt.Sprintf("unmarshal failed, cannot unmarshal %s into Go value type %s",
  20  		e.Value, e.Type.String())
  21  }
  22  
  23  // An InvalidUnmarshalError is an error type representing an invalid type
  24  // encountered while unmarshaling a Smithy document to a Go value type.
  25  type InvalidUnmarshalError struct {
  26  	Type reflect.Type
  27  }
  28  
  29  // Error returns the string representation of the error.
  30  // Satisfying the error interface.
  31  func (e *InvalidUnmarshalError) Error() string {
  32  	var msg string
  33  	if e.Type == nil {
  34  		msg = "cannot unmarshal to nil value"
  35  	} else if e.Type.Kind() != reflect.Ptr {
  36  		msg = fmt.Sprintf("cannot unmarshal to non-pointer value, got %s", e.Type.String())
  37  	} else {
  38  		msg = fmt.Sprintf("cannot unmarshal to nil value, %s", e.Type.String())
  39  	}
  40  
  41  	return fmt.Sprintf("unmarshal failed, %s", msg)
  42  }
  43  
  44  // An UnmarshalError wraps an error that occurred while unmarshaling a
  45  // Smithy document into a Go type. This is different from
  46  // UnmarshalTypeError in that it wraps the underlying error that occurred.
  47  type UnmarshalError struct {
  48  	Err   error
  49  	Value string
  50  	Type  reflect.Type
  51  }
  52  
  53  // Unwrap returns the underlying unmarshaling error
  54  func (e *UnmarshalError) Unwrap() error {
  55  	return e.Err
  56  }
  57  
  58  // Error returns the string representation of the error.
  59  // Satisfying the error interface.
  60  func (e *UnmarshalError) Error() string {
  61  	return fmt.Sprintf("unmarshal failed, cannot unmarshal %q into %s, %v",
  62  		e.Value, e.Type.String(), e.Err)
  63  }
  64  
  65  // An InvalidMarshalError is an error type representing an error
  66  // occurring when marshaling a Go value type.
  67  type InvalidMarshalError struct {
  68  	Message string
  69  }
  70  
  71  // Error returns the string representation of the error.
  72  // Satisfying the error interface.
  73  func (e *InvalidMarshalError) Error() string {
  74  	return fmt.Sprintf("marshal failed, %s", e.Message)
  75  }
  76