Skip to content

Commit 77ed337

Browse files
authored
fix: handle invalid input errors without details (#1453)
While it should never happen, some invalid input error are returned without details. In this case, we never return any diagnostics to the users and the provider fails with a unclear generic error. ``` │ Error: Missing Resource State After Create │ The Terraform Provider unexpectedly returned no resource state after having no errors in the resource creation. This is always an issue in the Terraform Provider and should be reported to │ the provider developers. │ │ The resource may have been successfully created, but Terraform is not tracking it. Applying the configuration again with no other action may result in duplicate resource errors. Import │ the resource if the resource was actually created and Terraform should be tracking it. ``` This fix now handle the case where no details/field were returned by the API.
1 parent cf7d203 commit 77ed337

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

internal/util/hcloudutil/error_framework.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ func APIErrorDiagnostics(err error) diag.Diagnostics {
2323

2424
if hcloud.IsError(hcloudErr, hcloud.ErrorCodeInvalidInput) {
2525
invalidInput := hcloudErr.Details.(hcloud.ErrorDetailsInvalidInput)
26+
27+
// Gracefully handle when invalid input details are not returned
28+
// by the API (should never happen).
29+
if len(invalidInput.Fields) == 0 {
30+
diagnostics.AddError(
31+
"Invalid field in API request",
32+
fmt.Sprintf(
33+
"An invalid field was encountered during an API request. "+
34+
"The field might not map 1:1 to your terraform resource.\n\n"+
35+
"%s\n\n"+
36+
"Error code: %s\n"+
37+
"%s",
38+
err.Error(), hcloudErr.Code, statusCodeMessage,
39+
))
40+
return diagnostics
41+
}
42+
2643
for _, field := range invalidInput.Fields {
2744
messages := make([]string, 0, len(field.Messages))
2845
for _, message := range field.Messages {

internal/util/hcloudutil/error_framework_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ Field: foobar
4949
Messages:
5050
- must be bar
5151
- foo too long
52+
Error code: invalid_input
53+
Status code: 400
54+
`),
55+
},
56+
},
57+
{
58+
name: "hcloud invalid input error without details",
59+
errRaw: map[string]any{
60+
"error": map[string]any{
61+
"code": "invalid_input",
62+
"message": "something is fishy",
63+
"details": nil,
64+
},
65+
},
66+
errStatusCode: http.StatusBadRequest,
67+
diagnostics: []diag.Diagnostic{
68+
diag.NewErrorDiagnostic(
69+
"Invalid field in API request",
70+
`An invalid field was encountered during an API request. The field might not map 1:1 to your terraform resource.
71+
72+
something is fishy (invalid_input)
73+
5274
Error code: invalid_input
5375
Status code: 400
5476
`),

0 commit comments

Comments
 (0)