Skip to content

Commit 529479c

Browse files
authored
Use errorcode values from the sdk rather than our own consts (#111)
1 parent 2466b51 commit 529479c

File tree

10 files changed

+47
-181
lines changed

10 files changed

+47
-181
lines changed

cfn/callback/callback.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,35 +116,23 @@ func (c *CloudFormationCallbackAdapter) reportProgress(errCode string, operation
116116
// TranslateErrorCode : Translate the error code into a standard Cloudformation error
117117
func TranslateErrorCode(errorCode string) string {
118118

119+
// Ensure the error code conforms to one of the available
119120
switch errorCode {
120-
case "NotUpdatable":
121-
return cloudformation.HandlerErrorCodeNotUpdatable
122-
case "InvalidRequest":
123-
return cloudformation.HandlerErrorCodeInvalidRequest
124-
case "AccessDenied":
125-
return cloudformation.HandlerErrorCodeAccessDenied
126-
case "InvalidCredentials":
127-
return cloudformation.HandlerErrorCodeInvalidCredentials
128-
case "AlreadyExists":
129-
return cloudformation.HandlerErrorCodeAlreadyExists
130-
case "NotFound":
131-
return cloudformation.HandlerErrorCodeNotFound
132-
case "ResourceConflict":
133-
return cloudformation.HandlerErrorCodeResourceConflict
134-
case "Throttling":
135-
return cloudformation.HandlerErrorCodeThrottling
136-
case "ServiceLimitExceeded":
137-
return cloudformation.HandlerErrorCodeServiceLimitExceeded
138-
case "NotStabilized":
139-
return cloudformation.HandlerErrorCodeNotStabilized
140-
case "GeneralServiceException":
141-
return cloudformation.HandlerErrorCodeGeneralServiceException
142-
case "ServiceInternalError":
143-
return cloudformation.HandlerErrorCodeServiceInternalError
144-
case "NetworkFailure":
145-
return cloudformation.HandlerErrorCodeNetworkFailure
146-
case "InternalFailure":
147-
return cloudformation.HandlerErrorCodeInternalFailure
121+
case cloudformation.HandlerErrorCodeNotUpdatable,
122+
cloudformation.HandlerErrorCodeInvalidRequest,
123+
cloudformation.HandlerErrorCodeAccessDenied,
124+
cloudformation.HandlerErrorCodeInvalidCredentials,
125+
cloudformation.HandlerErrorCodeAlreadyExists,
126+
cloudformation.HandlerErrorCodeNotFound,
127+
cloudformation.HandlerErrorCodeResourceConflict,
128+
cloudformation.HandlerErrorCodeThrottling,
129+
cloudformation.HandlerErrorCodeServiceLimitExceeded,
130+
cloudformation.HandlerErrorCodeNotStabilized,
131+
cloudformation.HandlerErrorCodeGeneralServiceException,
132+
cloudformation.HandlerErrorCodeServiceInternalError,
133+
cloudformation.HandlerErrorCodeNetworkFailure,
134+
cloudformation.HandlerErrorCodeInternalFailure:
135+
return errorCode
148136
default:
149137
// InternalFailure is CloudFormation's fallback error code when no more specificity is there
150138
return cloudformation.HandlerErrorCodeInternalFailure

cfn/callback/callback_notag.go

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,22 @@ func (c *CloudFormationCallbackAdapter) reportProgress(code string, operationSta
8383

8484
// TranslateErrorCode : Translate the error code into a standard Cloudformation error
8585
func TranslateErrorCode(errorCode string) string {
86-
8786
switch errorCode {
88-
case "NotUpdatable":
89-
return cloudformation.HandlerErrorCodeNotUpdatable
90-
case "InvalidRequest":
91-
return cloudformation.HandlerErrorCodeInvalidRequest
92-
case "AccessDenied":
93-
return cloudformation.HandlerErrorCodeAccessDenied
94-
case "InvalidCredentials":
95-
return cloudformation.HandlerErrorCodeInvalidCredentials
96-
case "AlreadyExists":
97-
return cloudformation.HandlerErrorCodeAlreadyExists
98-
case "NotFound":
99-
return cloudformation.HandlerErrorCodeNotFound
100-
case "ResourceConflict":
101-
return cloudformation.HandlerErrorCodeResourceConflict
102-
case "Throttling":
103-
return cloudformation.HandlerErrorCodeThrottling
104-
case "ServiceLimitExceeded":
105-
return cloudformation.HandlerErrorCodeServiceLimitExceeded
106-
case "NotStabilized":
107-
return cloudformation.HandlerErrorCodeNotStabilized
108-
case "GeneralServiceException":
109-
return cloudformation.HandlerErrorCodeGeneralServiceException
110-
case "ServiceInternalError":
111-
return cloudformation.HandlerErrorCodeServiceInternalError
112-
case "NetworkFailure":
113-
return cloudformation.HandlerErrorCodeNetworkFailure
114-
case "InternalFailure":
115-
return cloudformation.HandlerErrorCodeInternalFailure
87+
case cloudformation.HandlerErrorCodeNotUpdatable,
88+
cloudformation.HandlerErrorCodeInvalidRequest,
89+
cloudformation.HandlerErrorCodeAccessDenied,
90+
cloudformation.HandlerErrorCodeInvalidCredentials,
91+
cloudformation.HandlerErrorCodeAlreadyExists,
92+
cloudformation.HandlerErrorCodeNotFound,
93+
cloudformation.HandlerErrorCodeResourceConflict,
94+
cloudformation.HandlerErrorCodeThrottling,
95+
cloudformation.HandlerErrorCodeServiceLimitExceeded,
96+
cloudformation.HandlerErrorCodeNotStabilized,
97+
cloudformation.HandlerErrorCodeGeneralServiceException,
98+
cloudformation.HandlerErrorCodeServiceInternalError,
99+
cloudformation.HandlerErrorCodeNetworkFailure,
100+
cloudformation.HandlerErrorCodeInternalFailure:
101+
return errorCode
116102
default:
117103
// InternalFailure is CloudFormation's fallback error code when no more specificity is there
118104
return cloudformation.HandlerErrorCodeInternalFailure

cfn/cfn_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/scheduler"
1515
"github.com/aws/aws-lambda-go/lambdacontext"
1616
"github.com/aws/aws-sdk-go/aws/session"
17+
"github.com/aws/aws-sdk-go/service/cloudformation"
1718
)
1819

1920
func TestReschedule(t *testing.T) {
@@ -188,7 +189,7 @@ func TestMakeEventFunc(t *testing.T) {
188189
}, true},
189190
{"Test wrap panic", args{&MockHandler{f6}, context.Background(), loadEvent("request.create.json", &event{})}, response{
190191
OperationStatus: handler.Failed,
191-
ErrorCode: handler.GeneralServiceException,
192+
ErrorCode: cloudformation.HandlerErrorCodeGeneralServiceException,
192193
Message: "Unable to complete request: error",
193194
BearerToken: "123456",
194195
}, false},

cfn/cfnerr/consts.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

cfn/handler/errs.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

cfn/handler/event.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package handler
22

3-
import "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
3+
import (
4+
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
5+
"github.com/aws/aws-sdk-go/service/cloudformation"
6+
)
47

58
// ProgressEvent represent the progress of CRUD handlers.
69
type ProgressEvent struct {
@@ -9,7 +12,7 @@ type ProgressEvent struct {
912
OperationStatus Status `json:"status,omitempty"`
1013

1114
// HandlerErrorCode should be provided when OperationStatus is FAILED or IN_PROGRESS.
12-
HandlerErrorCode ErrorCode `json:"errorCode,omitempty"`
15+
HandlerErrorCode string `json:"errorCode,omitempty"`
1316

1417
// Message which can be shown to callers to indicate the
1518
//nature of a progress transition or callback delay; for example a message
@@ -50,14 +53,14 @@ func NewProgressEvent() ProgressEvent {
5053
// based on the error passed in.
5154
func NewFailedEvent(err error) ProgressEvent {
5255
cerr := cfnerr.New(
53-
cfnerr.GeneralServiceException,
56+
cloudformation.HandlerErrorCodeGeneralServiceException,
5457
"Unable to complete request: "+err.Error(),
5558
err,
5659
)
5760

5861
return ProgressEvent{
5962
OperationStatus: Failed,
6063
Message: cerr.Message(),
61-
HandlerErrorCode: GeneralServiceException,
64+
HandlerErrorCode: cloudformation.HandlerErrorCodeGeneralServiceException,
6265
}
6366
}

cfn/reportErr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/callback"
99
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
1010
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/metrics"
11+
"github.com/aws/aws-sdk-go/service/cloudformation"
1112
)
1213

1314
//reportErr is an unexported struct that handles reporting of errors.
@@ -31,7 +32,7 @@ func (r *reportErr) report(event *event, message string, err error, errCode stri
3132
m := fmt.Sprintf("Unable to complete request; %s error", message)
3233

3334
if isMutatingAction(event.Action) && r.publishStatus {
34-
if reportErr := r.callbackAdapter.ReportFailureStatus(event.RequestData.ResourceProperties, cfnerr.InternalFailure, err); reportErr != nil {
35+
if reportErr := r.callbackAdapter.ReportFailureStatus(event.RequestData.ResourceProperties, cloudformation.HandlerErrorCodeInternalFailure, err); reportErr != nil {
3536
log.Printf("Callback report error; Error: %s", reportErr.Error())
3637
}
3738
}

cfn/response.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cfn
22

33
import (
44
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler"
5+
"github.com/aws/aws-sdk-go/service/cloudformation"
56
)
67

78
// response represents a response to the
@@ -23,7 +24,7 @@ type response struct {
2324
ResourceModel interface{} `json:"resourceModel,omitempty"`
2425

2526
// ErrorCode is used to report granular failures back to CloudFormation
26-
ErrorCode handler.ErrorCode `json:"errorCode,omitempty"`
27+
ErrorCode string `json:"errorCode,omitempty"`
2728

2829
// BearerToken is used to report progress back to CloudFormation and is
2930
//passed back to CloudFormation
@@ -40,7 +41,7 @@ type response struct {
4041
func newFailedResponse(err error, bearerToken string) response {
4142
return response{
4243
OperationStatus: handler.Failed,
43-
ErrorCode: handler.InternalFailure,
44+
ErrorCode: cloudformation.HandlerErrorCodeInternalFailure,
4445
Message: err.Error(),
4546
BearerToken: bearerToken,
4647
}

cfn/response_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cfn
33
import (
44
"testing"
55

6+
"github.com/aws/aws-sdk-go/service/cloudformation"
67
"github.com/google/go-cmp/cmp"
78

89
"encoding/json"
@@ -24,7 +25,7 @@ func TestMarshalJSON(t *testing.T) {
2425
Name: encoding.NewString("Douglas"),
2526
Version: encoding.NewFloat(42.1),
2627
},
27-
ErrorCode: handler.NotUpdatable,
28+
ErrorCode: cloudformation.HandlerErrorCodeNotUpdatable,
2829
BearerToken: "xyzzy",
2930
}
3031

File renamed without changes.

0 commit comments

Comments
 (0)