-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add helper to catch API Errors
- Loading branch information
Showing
3 changed files
with
144 additions
and
31 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,98 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
apiclient "github.com/orange-cloudavenue/cloudavenue-sdk-go" | ||
) | ||
|
||
type APIError struct { | ||
LastError error | ||
StatusCode int | ||
apiclient.ApiError | ||
} | ||
|
||
func (e *APIError) Error() string { | ||
if e.Code != "" && e.Message != "" { | ||
return fmt.Sprintf("%s (HTTP Code:%s)", e.Message, e.Code) | ||
} | ||
|
||
if e.Message != "" { | ||
return e.Message | ||
} | ||
|
||
return "couldn't find resource" | ||
} | ||
|
||
func (e *APIError) Unwrap() error { | ||
return e.LastError | ||
} | ||
|
||
// GetSummary returns a summary of the error. | ||
func (e *APIError) GetSummary() string { | ||
return fmt.Sprintf("%s (HTTP Code:%d)", e.Reason, e.StatusCode) | ||
} | ||
|
||
// GetDetail returns a detailed description of the error. | ||
func (e *APIError) GetDetail() string { | ||
return e.Message | ||
} | ||
|
||
// GetTerraformDiagnostic returns a Terraform Diagnostic for the error. | ||
func (e *APIError) GetTerraformDiagnostic() diag.Diagnostic { | ||
var summary, detail string | ||
if e.Reason != "" { | ||
summary = e.GetSummary() | ||
} else { | ||
summary = fmt.Sprintf("HTTP response error: %d", e.StatusCode) | ||
} | ||
if e.Message != "" { | ||
detail = e.GetDetail() | ||
} else { | ||
detail = e.LastError.Error() | ||
} | ||
|
||
if e.IsNotFound() { | ||
return diag.NewWarningDiagnostic(summary, detail) | ||
} | ||
return diag.NewErrorDiagnostic(summary, detail) | ||
} | ||
|
||
// IsNotFound returns true if the error is a 404 Not Found error. | ||
func (e *APIError) IsNotFound() bool { | ||
return e.StatusCode == 404 | ||
} | ||
|
||
// CheckApiError checks the HTTP response for errors and returns an APIError | ||
// if the response code is >= 400. | ||
// If the response code is < 400, nil is returned. | ||
func CheckApiError(err error, httpR *http.Response) *APIError { | ||
|
||
if err == nil { | ||
return nil | ||
} | ||
|
||
if httpR != nil && httpR.StatusCode >= 400 { | ||
|
||
if e, ok := err.(apiclient.GenericSwaggerError); ok { | ||
x := &APIError{ | ||
LastError: err, | ||
StatusCode: httpR.StatusCode, | ||
} | ||
|
||
if e.Model() != nil { | ||
if m, ok := e.Model().(apiclient.ApiError); ok { | ||
x.ApiError = m | ||
} | ||
} | ||
|
||
return x | ||
} | ||
} else { | ||
return nil | ||
} | ||
|
||
return nil | ||
} |