sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/elb/errors.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package elb 18 19 import ( 20 "net/http" 21 22 "github.com/aws/aws-sdk-go/aws/awserr" 23 "github.com/aws/aws-sdk-go/service/elb" 24 "github.com/pkg/errors" 25 26 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/awserrors" 27 ) 28 29 var _ error = &ELBError{} 30 31 // ELBError is an error exposed to users of this library. 32 type ELBError struct { 33 msg string 34 35 Code int 36 } 37 38 // Error implements the Error interface. 39 func (e *ELBError) Error() string { 40 return e.msg 41 } 42 43 // NewNotFound returns an error which indicates that the resource of the kind and the name was not found. 44 func NewNotFound(msg string) error { 45 return &ELBError{ 46 msg: msg, 47 Code: http.StatusNotFound, 48 } 49 } 50 51 // NewConflict returns an error which indicates that the request cannot be processed due to a conflict. 52 func NewConflict(msg string) error { 53 return &ELBError{ 54 msg: msg, 55 Code: http.StatusConflict, 56 } 57 } 58 59 // IsNotFound returns true if the error was created by NewNotFound. 60 func IsNotFound(err error) bool { 61 if ReasonForError(err) == http.StatusNotFound { 62 return true 63 } 64 if code, ok := awserrors.Code(errors.Cause(err)); ok { 65 if code == elb.ErrCodeAccessPointNotFoundException { 66 return true 67 } 68 } 69 return false 70 } 71 72 // IsAccessDenied returns true if the error is AccessDenied. 73 func IsAccessDenied(err error) bool { 74 if code, ok := awserrors.Code(errors.Cause(err)); ok { 75 if code == "AccessDenied" { 76 return true 77 } 78 } 79 return false 80 } 81 82 // IsConflict returns true if the error was created by NewConflict. 83 func IsConflict(err error) bool { 84 return ReasonForError(err) == http.StatusConflict 85 } 86 87 // IsSDKError returns true if the error is of type awserr.Error. 88 func IsSDKError(err error) (ok bool) { 89 _, ok = errors.Cause(err).(awserr.Error) 90 return 91 } 92 93 // ReasonForError returns the HTTP status for a particular error. 94 func ReasonForError(err error) int { 95 if t, ok := errors.Cause(err).(*ELBError); ok { 96 return t.Code 97 } 98 return -1 99 }