This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitry Shmulevich
committed
Jan 24, 2018
1 parent
46698a3
commit 283ef52
Showing
10 changed files
with
325 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
package apierror | ||
|
||
// New creates an ErrorResponse | ||
func New(errorCategory ErrorCategory, errorCode ErrorCode, message string) *ErrorResponse { | ||
return &ErrorResponse{ | ||
Body: Error{ | ||
Code: errorCode, | ||
Message: message, | ||
Category: errorCategory, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
package apierror | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestNewAPIError(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
apiError := New( | ||
ClientError, | ||
InvalidParameter, | ||
"error test") | ||
|
||
Expect(apiError.Body.Code).Should(Equal(ErrorCode("InvalidParameter"))) | ||
} | ||
|
||
func TestAcsNewAPIError(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
apiError := New( | ||
ClientError, | ||
ScaleDownInternalError, | ||
"error test") | ||
|
||
Expect(apiError.Body.Code).Should(Equal(ErrorCode("ScaleDownInternalError"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
package apierror | ||
|
||
// ErrorCategory indicates the kind of error | ||
type ErrorCategory string | ||
|
||
const ( | ||
// ClientError is expected error | ||
ClientError ErrorCategory = "ClientError" | ||
|
||
// InternalError is system or internal error | ||
InternalError ErrorCategory = "InternalError" | ||
) | ||
|
||
// Common Azure Resource Provider API error code | ||
type ErrorCode string | ||
|
||
const ( | ||
// From Microsoft.Azure.ResourceProvider.API.ErrorCode | ||
InvalidParameter ErrorCode = "InvalidParameter" | ||
BadRequest ErrorCode = "BadRequest" | ||
NotFound ErrorCode = "NotFound" | ||
Conflict ErrorCode = "Conflict" | ||
PreconditionFailed ErrorCode = "PreconditionFailed" | ||
OperationNotAllowed ErrorCode = "OperationNotAllowed" | ||
OperationPreempted ErrorCode = "OperationPreempted" | ||
PropertyChangeNotAllowed ErrorCode = "PropertyChangeNotAllowed" | ||
InternalOperationError ErrorCode = "InternalOperationError" | ||
InvalidSubscriptionStateTransition ErrorCode = "InvalidSubscriptionStateTransition" | ||
UnregisterWithResourcesNotAllowed ErrorCode = "UnregisterWithResourcesNotAllowed" | ||
InvalidParameterConflictingProperties ErrorCode = "InvalidParameterConflictingProperties" | ||
SubscriptionNotRegistered ErrorCode = "SubscriptionNotRegistered" | ||
ConflictingUserInput ErrorCode = "ConflictingUserInput" | ||
ProvisioningInternalError ErrorCode = "ProvisioningInternalError" | ||
ProvisioningFailed ErrorCode = "ProvisioningFailed" | ||
NetworkingInternalOperationError ErrorCode = "NetworkingInternalOperationError" | ||
QuotaExceeded ErrorCode = "QuotaExceeded" | ||
Unauthorized ErrorCode = "Unauthorized" | ||
ResourcesOverConstrained ErrorCode = "ResourcesOverConstrained" | ||
ControlPlaneProvisioningInternalError ErrorCode = "ControlPlaneProvisioningInternalError" | ||
ControlPlaneProvisioningSyncError ErrorCode = "ControlPlaneProvisioningSyncError" | ||
ControlPlaneInternalError ErrorCode = "ControlPlaneInternalError" | ||
ControlPlaneCloudProviderNotSet ErrorCode = "ControlPlaneCloudProviderNotSet" | ||
|
||
// From Microsoft.WindowsAzure.ContainerService.API.AcsErrorCode | ||
ScaleDownInternalError ErrorCode = "ScaleDownInternalError" | ||
|
||
// New | ||
PreconditionCheckTimeOut ErrorCode = "PreconditionCheckTimeOut" | ||
UpgradeFailed ErrorCode = "UpgradeFailed" | ||
ScaleError ErrorCode = "ScaleError" | ||
CreateRoleAssignmentError ErrorCode = "CreateRoleAssignmentError" | ||
ServicePrincipalNotFound ErrorCode = "ServicePrincipalNotFound" | ||
ClusterResourceGroupNotFound ErrorCode = "ClusterResourceGroupNotFound" | ||
|
||
// Error codes returned by HCP | ||
UnderlayNotFound ErrorCode = "UnderlayNotFound" | ||
UnderlaysOverConstrained ErrorCode = "UnderlaysOverConstrained" | ||
UnexpectedUnderlayCount ErrorCode = "UnexpectedUnderlayCount" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package apierror | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/resources/resources" | ||
) | ||
|
||
// ExtractCodeFromARMHttpResponse returns the ARM error's Code field | ||
// If not found return defaultCode | ||
func ExtractCodeFromARMHttpResponse(resp *http.Response, defaultCode ErrorCode) ErrorCode { | ||
if resp == nil { | ||
return defaultCode | ||
} | ||
decoder := json.NewDecoder(resp.Body) | ||
errorJSON := ErrorResponse{} | ||
if err := decoder.Decode(&errorJSON); err != nil { | ||
return defaultCode | ||
} | ||
|
||
if errorJSON.Body.Code == "" { | ||
return defaultCode | ||
} | ||
return ErrorCode(errorJSON.Body.Code) | ||
} | ||
|
||
//ConvertToAPIError turns a ManagementErrorWithDetails into a apierror.Error | ||
func ConvertToAPIError(mError *resources.ManagementErrorWithDetails) *Error { | ||
retVal := &Error{} | ||
if mError.Code != nil { | ||
retVal.Code = ErrorCode(*mError.Code) | ||
} | ||
if mError.Message != nil { | ||
retVal.Message = *mError.Message | ||
} | ||
if mError.Target != nil { | ||
retVal.Target = *mError.Target | ||
} | ||
if mError.Details != nil { | ||
retVal.Details = []Error{} | ||
for _, me := range *mError.Details { | ||
retVal.Details = append(retVal.Details, *ConvertToAPIError(&me)) | ||
} | ||
} | ||
return retVal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package apierror | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestExtractCodeFromARMHttpResponse(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
resp := &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":{"code":"ResourceGroupNotFound","message":"Resource group 'jiren-fakegroup' could not be found."}}`)), | ||
} | ||
|
||
code := ExtractCodeFromARMHttpResponse(resp, "") | ||
Expect(code).To(Equal(ErrorCode("ResourceGroupNotFound"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
package apierror | ||
|
||
import "encoding/json" | ||
|
||
// Error is the OData v4 format, used by the RPC and | ||
// will go into the v2.2 Azure REST API guidelines | ||
type Error struct { | ||
Code ErrorCode `json:"code"` | ||
Message string `json:"message"` | ||
Target string `json:"target,omitempty"` | ||
Details []Error `json:"details,omitempty"` | ||
|
||
Category ErrorCategory `json:"-"` | ||
ExceptionType string `json:"-"` | ||
InternalDetail string `json:"-"` | ||
} | ||
|
||
// ErrorResponse defines Resource Provider API 2.0 Error Response Content structure | ||
type ErrorResponse struct { | ||
Body Error `json:"error"` | ||
} | ||
|
||
// Error implements error interface to return error in json | ||
func (e *ErrorResponse) Error() string { | ||
return e.Body.Error() | ||
} | ||
|
||
// Error implements error interface to return error in json | ||
func (e *Error) Error() string { | ||
output, err := json.MarshalIndent(e, " ", " ") | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return string(output) | ||
} |
Oops, something went wrong.