src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

errors.go (3916B)


      1 package smithy
      2 
      3 import "fmt"
      4 
      5 // APIError provides the generic API and protocol agnostic error type all SDK
      6 // generated exception types will implement.
      7 type APIError interface {
      8 	error
      9 
     10 	// ErrorCode returns the error code for the API exception.
     11 	ErrorCode() string
     12 	// ErrorMessage returns the error message for the API exception.
     13 	ErrorMessage() string
     14 	// ErrorFault returns the fault for the API exception.
     15 	ErrorFault() ErrorFault
     16 }
     17 
     18 // GenericAPIError provides a generic concrete API error type that SDKs can use
     19 // to deserialize error responses into. Should be used for unmodeled or untyped
     20 // errors.
     21 type GenericAPIError struct {
     22 	Code    string
     23 	Message string
     24 	Fault   ErrorFault
     25 }
     26 
     27 // ErrorCode returns the error code for the API exception.
     28 func (e *GenericAPIError) ErrorCode() string { return e.Code }
     29 
     30 // ErrorMessage returns the error message for the API exception.
     31 func (e *GenericAPIError) ErrorMessage() string { return e.Message }
     32 
     33 // ErrorFault returns the fault for the API exception.
     34 func (e *GenericAPIError) ErrorFault() ErrorFault { return e.Fault }
     35 
     36 func (e *GenericAPIError) Error() string {
     37 	return fmt.Sprintf("api error %s: %s", e.Code, e.Message)
     38 }
     39 
     40 var _ APIError = (*GenericAPIError)(nil)
     41 
     42 // OperationError decorates an underlying error which occurred while invoking
     43 // an operation with names of the operation and API.
     44 type OperationError struct {
     45 	ServiceID     string
     46 	OperationName string
     47 	Err           error
     48 }
     49 
     50 // Service returns the name of the API service the error occurred with.
     51 func (e *OperationError) Service() string { return e.ServiceID }
     52 
     53 // Operation returns the name of the API operation the error occurred with.
     54 func (e *OperationError) Operation() string { return e.OperationName }
     55 
     56 // Unwrap returns the nested error if any, or nil.
     57 func (e *OperationError) Unwrap() error { return e.Err }
     58 
     59 func (e *OperationError) Error() string {
     60 	return fmt.Sprintf("operation error %s: %s, %v", e.ServiceID, e.OperationName, e.Err)
     61 }
     62 
     63 // DeserializationError provides a wrapper for an error that occurs during
     64 // deserialization.
     65 type DeserializationError struct {
     66 	Err      error //  original error
     67 	Snapshot []byte
     68 }
     69 
     70 // Error returns a formatted error for DeserializationError
     71 func (e *DeserializationError) Error() string {
     72 	const msg = "deserialization failed"
     73 	if e.Err == nil {
     74 		return msg
     75 	}
     76 	return fmt.Sprintf("%s, %v", msg, e.Err)
     77 }
     78 
     79 // Unwrap returns the underlying Error in DeserializationError
     80 func (e *DeserializationError) Unwrap() error { return e.Err }
     81 
     82 // ErrorFault provides the type for a Smithy API error fault.
     83 type ErrorFault int
     84 
     85 // ErrorFault enumeration values
     86 const (
     87 	FaultUnknown ErrorFault = iota
     88 	FaultServer
     89 	FaultClient
     90 )
     91 
     92 func (f ErrorFault) String() string {
     93 	switch f {
     94 	case FaultServer:
     95 		return "server"
     96 	case FaultClient:
     97 		return "client"
     98 	default:
     99 		return "unknown"
    100 	}
    101 }
    102 
    103 // SerializationError represents an error that occurred while attempting to serialize a request
    104 type SerializationError struct {
    105 	Err error // original error
    106 }
    107 
    108 // Error returns a formatted error for SerializationError
    109 func (e *SerializationError) Error() string {
    110 	const msg = "serialization failed"
    111 	if e.Err == nil {
    112 		return msg
    113 	}
    114 	return fmt.Sprintf("%s: %v", msg, e.Err)
    115 }
    116 
    117 // Unwrap returns the underlying Error in SerializationError
    118 func (e *SerializationError) Unwrap() error { return e.Err }
    119 
    120 // CanceledError is the error that will be returned by an API request that was
    121 // canceled. API operations given a Context may return this error when
    122 // canceled.
    123 type CanceledError struct {
    124 	Err error
    125 }
    126 
    127 // CanceledError returns true to satisfy interfaces checking for canceled errors.
    128 func (*CanceledError) CanceledError() bool { return true }
    129 
    130 // Unwrap returns the underlying error, if there was one.
    131 func (e *CanceledError) Unwrap() error {
    132 	return e.Err
    133 }
    134 
    135 func (e *CanceledError) Error() string {
    136 	return fmt.Sprintf("canceled, %v", e.Err)
    137 }