github.com/sfdevops1/terrra4orm@v0.11.12-beta1/helper/resource/error.go (about) 1 package resource 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 ) 8 9 type NotFoundError struct { 10 LastError error 11 LastRequest interface{} 12 LastResponse interface{} 13 Message string 14 Retries int 15 } 16 17 func (e *NotFoundError) Error() string { 18 if e.Message != "" { 19 return e.Message 20 } 21 22 if e.Retries > 0 { 23 return fmt.Sprintf("couldn't find resource (%d retries)", e.Retries) 24 } 25 26 return "couldn't find resource" 27 } 28 29 // UnexpectedStateError is returned when Refresh returns a state that's neither in Target nor Pending 30 type UnexpectedStateError struct { 31 LastError error 32 State string 33 ExpectedState []string 34 } 35 36 func (e *UnexpectedStateError) Error() string { 37 return fmt.Sprintf( 38 "unexpected state '%s', wanted target '%s'. last error: %s", 39 e.State, 40 strings.Join(e.ExpectedState, ", "), 41 e.LastError, 42 ) 43 } 44 45 // TimeoutError is returned when WaitForState times out 46 type TimeoutError struct { 47 LastError error 48 LastState string 49 Timeout time.Duration 50 ExpectedState []string 51 } 52 53 func (e *TimeoutError) Error() string { 54 expectedState := "resource to be gone" 55 if len(e.ExpectedState) > 0 { 56 expectedState = fmt.Sprintf("state to become '%s'", strings.Join(e.ExpectedState, ", ")) 57 } 58 59 extraInfo := make([]string, 0) 60 if e.LastState != "" { 61 extraInfo = append(extraInfo, fmt.Sprintf("last state: '%s'", e.LastState)) 62 } 63 if e.Timeout > 0 { 64 extraInfo = append(extraInfo, fmt.Sprintf("timeout: %s", e.Timeout.String())) 65 } 66 67 suffix := "" 68 if len(extraInfo) > 0 { 69 suffix = fmt.Sprintf(" (%s)", strings.Join(extraInfo, ", ")) 70 } 71 72 if e.LastError != nil { 73 return fmt.Sprintf("timeout while waiting for %s%s: %s", 74 expectedState, suffix, e.LastError) 75 } 76 77 return fmt.Sprintf("timeout while waiting for %s%s", 78 expectedState, suffix) 79 }