-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: standardise error handling (#539)
* Providing a standardise way to handle errors * Fixing up error handling * Renaming to handle error
- Loading branch information
1 parent
e482fda
commit 180a08c
Showing
4 changed files
with
116 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/signalfx/signalfx-go" | ||
|
||
tfext "github.com/splunk-terraform/terraform-provider-signalfx/internal/tfextension" | ||
) | ||
|
||
// HandleError handles the general case when the signalfx api returns | ||
// an error, and it uses that information to determine what needs to happen. | ||
// This will ensure that the state is cleaned up given the error condition. | ||
// To help simplify error handling, it will always return the error provided. | ||
func HandleError(ctx context.Context, err error, data *schema.ResourceData) error { | ||
re, ok := signalfx.AsResponseError(err) | ||
if !ok { | ||
// Not a response error, pass it back | ||
return err | ||
} | ||
switch re.Code() { | ||
case http.StatusNotFound: | ||
tflog.Info(ctx, "Resource has been removed externally, removing from state", tfext.NewLogFields(). | ||
Field("route", re.Route()), | ||
) | ||
// Clear the id from the state when 404 is returned. | ||
data.SetId("") | ||
case http.StatusUnauthorized: | ||
tflog.Error(ctx, "Token is not authorized", tfext.NewLogFields(). | ||
Field("route", re.Route()). | ||
Field("code", re.Code()). | ||
Field("details", re.Details()), | ||
) | ||
default: | ||
tflog.Debug(ctx, "Issue trying to work with the API", tfext.NewLogFields(). | ||
Field("route", re.Route()). | ||
Field("code", re.Code()). | ||
Field("details", re.Details()), | ||
) | ||
} | ||
return err | ||
} |
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,54 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/signalfx/signalfx-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOnError(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
err error | ||
expect string | ||
}{ | ||
{ | ||
name: "no error provided", | ||
err: nil, | ||
expect: "id", | ||
}, | ||
{ | ||
name: "not a response rror", | ||
err: errors.New("derp"), | ||
expect: "id", | ||
}, | ||
{ | ||
name: "uncatalogue response error", | ||
err: &signalfx.ResponseError{}, | ||
expect: "id", | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
data := schema.TestResourceDataRaw( | ||
t, | ||
map[string]*schema.Schema{}, | ||
map[string]any{}, | ||
) | ||
data.SetId("id") | ||
|
||
assert.ErrorIs(t, tc.err, HandleError(context.Background(), tc.err, data), "Must return the same error") | ||
assert.Equal(t, tc.expect, data.Id(), "Must have the expected id") | ||
}) | ||
} | ||
} |
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