-
Notifications
You must be signed in to change notification settings - Fork 76
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
Showing
4 changed files
with
57 additions
and
0 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,56 @@ | ||
package hcclient | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
|
||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
) | ||
|
||
func APIErrorDiagnostic(resource string, err error) diag.Diagnostics { | ||
var diagnostics diag.Diagnostics | ||
var hcloudErr hcloud.Error | ||
|
||
if errors.As(err, &hcloudErr) { | ||
if hcloud.IsError(hcloudErr, hcloud.ErrorCodeInvalidInput) { | ||
ie := hcloudErr.Details.(hcloud.ErrorDetailsInvalidInput) | ||
for _, v := range ie.Fields { | ||
diagnostics.AddError( | ||
"Invalid field in API request", | ||
fmt.Sprintf( | ||
`An invalid field was encountered while doing the request to %s. The field might not map 1:1 to your terraform resource. | ||
%s => %s | ||
Error code: %s | ||
`, | ||
resource, v.Name, v.Messages, hcloudErr.Code, | ||
)) | ||
} | ||
return diagnostics | ||
} | ||
|
||
diagnostics.AddError( | ||
fmt.Sprintf("Request to %s failed", resource), | ||
fmt.Sprintf( | ||
`An unexpected error was encountered while doing the request to %s. | ||
%s | ||
Error code: %s`, | ||
resource, hcloudErr.Message, hcloudErr.Code, | ||
), | ||
) | ||
return diagnostics | ||
} | ||
|
||
diagnostics.AddError( | ||
fmt.Sprintf("Request to %s failed", resource), | ||
fmt.Sprintf( | ||
"An unexpected error was encountered while doing the request to %s.\n\n%s\n", | ||
resource, err.Error(), | ||
), | ||
) | ||
return diagnostics | ||
} |
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 @@ | ||
package hcclient |
File renamed without changes.
File renamed without changes.