Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ErrorDetails into a pointer #8163

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/armrpc/api/v1/errorresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ package v1

// ErrorResponse represents an error HTTP response as defined by the ARM API.
type ErrorResponse struct {
Error ErrorDetails `json:"error"`
Error *ErrorDetails `json:"error"`
}

// ErrorDetails represents an error as defined by the ARM API.
type ErrorDetails struct {
Code string `json:"code"`
Message string `json:"message"`
Target string `json:"target,omitempty"`
AdditionalInfo []ErrorAdditionalInfo `json:"additionalInfo,omitempty"`
Details []ErrorDetails `json:"details,omitempty"`
Code string `json:"code"`
Message string `json:"message"`
Target string `json:"target,omitempty"`
AdditionalInfo []*ErrorAdditionalInfo `json:"additionalInfo,omitempty"`
Details []*ErrorDetails `json:"details,omitempty"`
}

// Error returns error message in ErrorDetails to implement error interface.
func (e ErrorDetails) Error() string {
func (e *ErrorDetails) Error() string {
return e.Message
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/armrpc/authentication/certvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func handleErr(ctx context.Context, w http.ResponseWriter, req *http.Request) {
if err != nil {
// Responds with an HTTP 500
body := v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInternal,
Message: err.Error(),
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/armrpc/frontend/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,29 +277,29 @@ func HandleError(ctx context.Context, w http.ResponseWriter, req *http.Request,
switch v := err.(type) {
case *v1.ErrModelConversion:
response = rest.NewBadRequestARMResponse(v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeHTTPRequestPayloadAPISpecValidationFailed,
Message: err.Error(),
},
})
case *v1.ErrClientRP:
response = rest.NewBadRequestARMResponse(v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v.Code,
Message: v.Message,
},
})
default:
if errors.Is(err, v1.ErrInvalidModelConversion) {
response = rest.NewBadRequestARMResponse(v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeHTTPRequestPayloadAPISpecValidationFailed,
Message: err.Error(),
},
})
} else {
response = rest.NewInternalServerErrorARMResponse(v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInternal,
Message: err.Error(),
},
Expand All @@ -309,8 +309,8 @@ func HandleError(ctx context.Context, w http.ResponseWriter, req *http.Request,

err = response.Apply(ctx, w, req)
if err != nil {
body := v1.ErrorResponse{
Error: v1.ErrorDetails{
body := &v1.ErrorResponse{
Error: &v1.ErrorDetails{
Code: v1.CodeInternal,
Message: err.Error(),
},
Expand Down
26 changes: 13 additions & 13 deletions pkg/armrpc/rest/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func NewLinkedResourceUpdateErrorResponse(resourceID resources.ID, oldProp *rpv1
message := fmt.Sprintf(LinkedResourceUpdateErrorFormat, resourceID.Name(), resourceID.Name(), newAppEnv, oldProp.Application, oldProp.Environment, resourceID.Name())
return &BadRequestResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: message,
Target: resourceID.String(),
Expand All @@ -411,7 +411,7 @@ func NewLinkedResourceUpdateErrorResponse(resourceID resources.ID, oldProp *rpv1
func NewDependencyMissingResponse(message string) Response {
return &BadRequestResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeDependencyMissing,
Message: message,
},
Expand All @@ -423,7 +423,7 @@ func NewDependencyMissingResponse(message string) Response {
func NewBadRequestResponse(message string) Response {
return &BadRequestResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: message,
},
Expand Down Expand Up @@ -466,15 +466,15 @@ type ValidationErrorResponse struct {
// NewValidationErrorResponse creates a BadRequest response for invalid API validation.
func NewValidationErrorResponse(errors validator.ValidationErrors) Response {
body := v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: errors.Error(),
},
}

for _, fe := range errors {
if err, ok := fe.(error); ok {
detail := v1.ErrorDetails{
detail := &v1.ErrorDetails{
Target: fe.Field(),
Message: err.Error(),
}
Expand Down Expand Up @@ -516,7 +516,7 @@ type NotFoundResponse struct {
func NewNotFoundMessageResponse(message string) Response {
return &NotFoundResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeNotFound,
Message: message,
},
Expand All @@ -528,7 +528,7 @@ func NewNotFoundMessageResponse(message string) Response {
func NewNotFoundResponse(id resources.ID) Response {
return &NotFoundResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeNotFound,
Message: fmt.Sprintf("the resource with id '%s' was not found", id.String()),
Target: id.String(),
Expand All @@ -541,7 +541,7 @@ func NewNotFoundResponse(id resources.ID) Response {
func NewNotFoundResponseWithCause(id resources.ID, cause string) Response {
return &NotFoundResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeNotFound,
Message: fmt.Sprintf("the resource with id '%s' was not found: %s", id.String(), cause),
Target: id.String(),
Expand All @@ -554,7 +554,7 @@ func NewNotFoundResponseWithCause(id resources.ID, cause string) Response {
func NewNotFoundAPIVersionResponse(resourceType string, namespace string, apiVersion string) Response {
return &NotFoundResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalidResourceType, // ARM uses "InvalidResourceType" code with 404 http code.
Message: fmt.Sprintf("The resource type '%s' could not be found in the namespace '%s' for api version '%s'.", resourceType, namespace, apiVersion),
},
Expand Down Expand Up @@ -593,7 +593,7 @@ type ConflictResponse struct {
func NewConflictResponse(message string) Response {
return &ConflictResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeConflict,
Message: message,
},
Expand Down Expand Up @@ -661,7 +661,7 @@ type PreconditionFailedResponse struct {
func NewPreconditionFailedResponse(target string, message string) Response {
return &PreconditionFailedResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodePreconditionFailed,
Message: message,
Target: target,
Expand Down Expand Up @@ -699,7 +699,7 @@ type ClientAuthenticationFailed struct {
func NewClientAuthenticationFailedARMResponse() Response {
return &ClientAuthenticationFailed{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalidAuthenticationInfo,
Message: "Server failed to authenticate the request",
},
Expand Down Expand Up @@ -767,7 +767,7 @@ type MethodNotAllowedResponse struct {
func NewMethodNotAllowedResponse(target string, message string) Response {
return &MethodNotAllowedResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: message,
Target: target,
Expand Down
2 changes: 1 addition & 1 deletion pkg/armrpc/rest/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Test_NewLinkedResourceUpdateErrorResponse(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
expctedResp := &BadRequestResponse{
Body: v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: tt.msg,
Target: resource.String(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/armrpc/servicecontext/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ARMRequestCtx(pathBase, location string) func(h http.Handler) http.Handler
rpcContext, err := v1.FromARMRequest(r, pathBase, location)
if err != nil {
resp := rest.NewBadRequestARMResponse(v1.ErrorResponse{
Error: v1.ErrorDetails{
Error: &v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: fmt.Sprintf("unexpected error: %v", err),
},
Expand Down
26 changes: 13 additions & 13 deletions pkg/corerp/frontend/controller/containers/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func ValidateAndMutateRequest(ctx context.Context, newResource, oldResource *dat
if runtimes.Kubernetes.Base != "" {
err := validateBaseManifest([]byte(runtimes.Kubernetes.Base), newResource)
if err != nil {
return rest.NewBadRequestARMResponse(v1.ErrorResponse{Error: err.(v1.ErrorDetails)}), nil
return rest.NewBadRequestARMResponse(v1.ErrorResponse{Error: err.(*v1.ErrorDetails)}), nil
}
}

if runtimes.Kubernetes.Pod != "" {
err := validatePodSpec([]byte(runtimes.Kubernetes.Pod))
if err != nil {
return rest.NewBadRequestARMResponse(v1.ErrorResponse{Error: err.(v1.ErrorDetails)}), nil
return rest.NewBadRequestARMResponse(v1.ErrorResponse{Error: err.(*v1.ErrorDetails)}), nil
}
}
}
Expand All @@ -79,7 +79,7 @@ func validatePodSpec(patch []byte) error {
podSpec := &corev1.PodSpec{}
err := json.Unmarshal(patch, podSpec)
if err != nil {
return v1.ErrorDetails{
return &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: podTargetProperty,
Message: fmt.Sprintf("Invalid PodSpec for patching: %s.", err.Error()),
Expand All @@ -88,20 +88,20 @@ func validatePodSpec(patch []byte) error {
return nil
}

func errMultipleResources(typeName string, num int) v1.ErrorDetails {
return v1.ErrorDetails{
func errMultipleResources(typeName string, num int) *v1.ErrorDetails {
return &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: fmt.Sprintf("only one %s is allowed, but the manifest includes %d resources.", typeName, num),
}
}

func errUnmatchedName(obj runtime.Object, name string) v1.ErrorDetails {
func errUnmatchedName(obj runtime.Object, name string) *v1.ErrorDetails {
meta := obj.(metav1.ObjectMetaAccessor)
typeName := obj.GetObjectKind().GroupVersionKind().Kind
resourceName := meta.GetObjectMeta().GetName()

return v1.ErrorDetails{
return &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: fmt.Sprintf("%s name %s in manifest does not match resource name %s.", typeName, resourceName, name),
Expand All @@ -118,11 +118,11 @@ func errUnmatchedName(obj runtime.Object, name string) v1.ErrorDetails {
// - ConfigMap : 0-N
// - Secret : 0-N
func validateBaseManifest(manifest []byte, newResource *datamodel.ContainerResource) error {
errDetails := []v1.ErrorDetails{}
errDetails := []*v1.ErrorDetails{}

resourceMap, err := kubeutil.ParseManifest(manifest)
if err != nil {
return v1.ErrorDetails{
return &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: err.Error(),
Expand All @@ -135,7 +135,7 @@ func validateBaseManifest(manifest []byte, newResource *datamodel.ContainerResou
for _, resource := range resources {
meta := resource.(metav1.ObjectMetaAccessor)
if meta.GetObjectMeta().GetNamespace() != "" {
errDetails = append(errDetails, v1.ErrorDetails{
errDetails = append(errDetails, &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: fmt.Sprintf("namespace is not allowed in resources: %s.", meta.GetObjectMeta().GetNamespace()),
Expand Down Expand Up @@ -179,7 +179,7 @@ func validateBaseManifest(manifest []byte, newResource *datamodel.ContainerResou

podSA := deployment.(*appsv1.Deployment).Spec.Template.Spec.ServiceAccountName
if podSA != sa.Name {
errDetails = append(errDetails, v1.ErrorDetails{
errDetails = append(errDetails, &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: fmt.Sprintf("ServiceAccount name %s in PodSpec does not match the name %s in ServiceAccount.", podSA, sa.Name),
Expand All @@ -191,7 +191,7 @@ func validateBaseManifest(manifest []byte, newResource *datamodel.ContainerResou
case corev1.SchemeGroupVersion.WithKind("ConfigMap"):

default:
errDetails = append(errDetails, v1.ErrorDetails{
errDetails = append(errDetails, &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: fmt.Sprintf("%s is not supported.", k),
Expand All @@ -200,7 +200,7 @@ func validateBaseManifest(manifest []byte, newResource *datamodel.ContainerResou
}

if len(errDetails) > 0 {
return v1.ErrorDetails{
return &v1.ErrorDetails{
Code: v1.CodeInvalidRequestContent,
Target: manifestTargetProperty,
Message: "The manifest includes invalid resources.",
Expand Down
Loading
Loading