Skip to content

Commit

Permalink
chore: moved the error message to a common package and updated comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ShocOne committed Jul 25, 2024
1 parent c6da38c commit 545bfd9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
64 changes: 64 additions & 0 deletions internal/resources/common/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package common

import (
"strings"

"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
)

// IsNotFoundError checks if the given error is an OData error indicating that a resource was not found.
// The function first verifies if the error is not nil. Then, it attempts to cast the error to an ODataError
// type from the Microsoft Graph SDK. If the casting is successful, the function retrieves the main error
// details using the GetErrorEscaped method of the ODataError struct. It then checks if the error code or
// message contains indications of a "not found" error.
//
// Specifically, the function looks for the error codes "request_resourcenotfound" and "resourcenotfound"
// (case-insensitive), or a message containing the phrase "not found" (case-insensitive). If any of these
// conditions are met, the function returns true, indicating that the error is a "not found" error.
// Otherwise, it returns false.
//
// The ODataError struct is part of the Microsoft Graph SDK and includes various methods and properties
// to handle API errors. The main error details are encapsulated in a nested structure that provides
// additional context, such as error codes and descriptive messages.
//
// Usage:
//
// if common.IsNotFoundError(err) {
// // Handle the "not found" error case
// }
//
// Parameters:
//
// err - The error to check.
//
// Returns:
//
// bool - True if the error indicates that a resource was not found, otherwise false.
func IsNotFoundError(err error) bool {
if err == nil {
return false
}

odataErr, ok := err.(*odataerrors.ODataError)
if !ok {
return false
}

mainError := odataErr.GetErrorEscaped()
if mainError != nil {
if code := mainError.GetCode(); code != nil {
switch strings.ToLower(*code) {
case "request_resourcenotfound", "resourcenotfound":
return true
}
}

if message := mainError.GetMessage(); message != nil {
if strings.Contains(strings.ToLower(*message), "not found") {
return true
}
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package graphBetaAssignmentFilter
import (
"context"
"fmt"
"strings"
"time"

"github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
)

// Create handles the Create operation.
Expand Down Expand Up @@ -84,7 +83,7 @@ func (r *AssignmentFilterResource) Read(ctx context.Context, req resource.ReadRe

remoteResource, err := r.client.DeviceManagement().AssignmentFilters().ByDeviceAndAppManagementAssignmentFilterId(data.ID.ValueString()).Get(ctx, nil)
if err != nil {
if isNotFoundError(err) && !r.isCreate {
if common.IsNotFoundError(err) && !r.isCreate {
resp.Diagnostics.AddWarning(
"Resource Not Found",
fmt.Sprintf("The resource: %s_%s with ID %s was not found and will be removed from the state.", r.ProviderTypeName, r.TypeName, data.ID.ValueString()),
Expand Down Expand Up @@ -134,7 +133,7 @@ func (r *AssignmentFilterResource) Update(ctx context.Context, req resource.Upda

_, err = r.client.DeviceManagement().AssignmentFilters().ByDeviceAndAppManagementAssignmentFilterId(data.ID.ValueString()).Patch(ctx, requestBody, nil)
if err != nil {
if isNotFoundError(err) && !r.isCreate {
if common.IsNotFoundError(err) && !r.isCreate {
resp.Diagnostics.AddWarning(
"Resource Not Found",
fmt.Sprintf("The resource: %s_%s with ID %s was not found and will be removed from the state.", r.ProviderTypeName, r.TypeName, data.ID.ValueString()),
Expand Down Expand Up @@ -183,33 +182,3 @@ func (r *AssignmentFilterResource) Delete(ctx context.Context, req resource.Dele

resp.State.RemoveResource(ctx)
}

// isNotFoundError checks if the error is a not found error.
func isNotFoundError(err error) bool {
if err == nil {
return false
}

odataErr, ok := err.(*odataerrors.ODataError)
if !ok {
return false
}

mainError := odataErr.GetErrorEscaped()
if mainError != nil {
if code := mainError.GetCode(); code != nil {
switch strings.ToLower(*code) {
case "request_resourcenotfound", "resourcenotfound":
return true
}
}

if message := mainError.GetMessage(); message != nil {
if strings.Contains(strings.ToLower(*message), "not found") {
return true
}
}
}

return false
}

0 comments on commit 545bfd9

Please sign in to comment.