forked from labd/terraform-provider-contentful
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
c421212
commit bf76f6b
Showing
17 changed files
with
512 additions
and
189 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
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,34 @@ | ||
package contentful | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/labd/contentful-go" | ||
"strings" | ||
) | ||
|
||
func parseError(err error) diag.Diagnostics { | ||
if !errors.As(err, &contentful.ErrorResponse{}) { | ||
return diag.FromErr(err) | ||
} | ||
|
||
var warnings []diag.Diagnostic | ||
for _, e := range err.(contentful.ErrorResponse).Details.Errors { | ||
var path []string | ||
if e.Path != nil { | ||
for _, p := range e.Path.([]interface{}) { | ||
path = append(path, fmt.Sprintf("%v", p)) | ||
} | ||
} | ||
warnings = append(warnings, diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: fmt.Sprintf("%s (%s)", e.Details, strings.Join(path, ".")), | ||
}) | ||
} | ||
|
||
return append(warnings, diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: err.(contentful.ErrorResponse).Message, | ||
}) | ||
} |
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,69 @@ | ||
package contentful | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/labd/contentful-go" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseError_Nil(t *testing.T) { | ||
d := parseError(nil) | ||
assert.Nil(t, d) | ||
} | ||
|
||
func TestParseError_RegularErr(t *testing.T) { | ||
d := parseError(fmt.Errorf("regular error")) | ||
assert.True(t, d.HasError()) | ||
assert.Equal(t, d[0].Summary, "regular error") | ||
} | ||
|
||
func TestParseError_WithoutWarning(t *testing.T) { | ||
d := parseError(&contentful.ErrorResponse{ | ||
Message: "error message", | ||
}) | ||
assert.True(t, d.HasError()) | ||
assert.Equal(t, len(d), 1) | ||
assert.Equal(t, d[0].Summary, "error message") | ||
assert.Equal(t, d[0].Severity, diag.Error) | ||
} | ||
|
||
func TestParseError_WithWarning_WithoutPath(t *testing.T) { | ||
d := parseError(contentful.ErrorResponse{ | ||
Message: "error message", | ||
Details: &contentful.ErrorDetails{ | ||
Errors: []*contentful.ErrorDetail{ | ||
{ | ||
Details: "error detail", | ||
}, | ||
}, | ||
}, | ||
}) | ||
assert.True(t, d.HasError()) | ||
assert.Equal(t, len(d), 2) | ||
assert.Equal(t, d[0].Summary, "error detail ()") | ||
assert.Equal(t, d[0].Severity, diag.Warning) | ||
assert.Equal(t, d[1].Summary, "error message") | ||
assert.Equal(t, d[1].Severity, diag.Error) | ||
} | ||
|
||
func TestParseError_WithWarning_WithPath(t *testing.T) { | ||
d := parseError(contentful.ErrorResponse{ | ||
Message: "error message", | ||
Details: &contentful.ErrorDetails{ | ||
Errors: []*contentful.ErrorDetail{ | ||
{ | ||
Path: []interface{}{"path", "to", "error"}, | ||
Details: "error detail", | ||
}, | ||
}, | ||
}, | ||
}) | ||
assert.True(t, d.HasError()) | ||
assert.Equal(t, len(d), 2) | ||
assert.Equal(t, d[0].Summary, "error detail (path.to.error)") | ||
assert.Equal(t, d[0].Severity, diag.Warning) | ||
assert.Equal(t, d[1].Summary, "error message") | ||
assert.Equal(t, d[1].Severity, diag.Error) | ||
} |
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
Oops, something went wrong.