Skip to content

Commit

Permalink
fix: improved error message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thulasirajkomminar committed Sep 26, 2024
1 parent ede59f3 commit 9992292
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 18 deletions.
10 changes: 9 additions & 1 deletion internal/provider/database_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,17 @@ func (d *DatabaseDataSource) Read(ctx context.Context, req datasource.ReadReques
}

if readDatabasesResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(readDatabasesResponse, readDatabasesResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error getting database",
fmt.Sprintf("Status: %s", readDatabasesResponse.Status()),
errMsg,
)
return
}
Expand Down
40 changes: 36 additions & 4 deletions internal/provider/database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,17 @@ func (r *DatabaseResource) Create(ctx context.Context, req resource.CreateReques
}

if createDatabaseResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(createDatabaseResponse, createDatabaseResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error creating database",
fmt.Sprintf("Status: %s", createDatabaseResponse.Status()),
errMsg,
)
return
}
Expand Down Expand Up @@ -243,9 +251,17 @@ func (r *DatabaseResource) Read(ctx context.Context, req resource.ReadRequest, r
}

if readDatabasesResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(readDatabasesResponse, readDatabasesResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error getting database",
fmt.Sprintf("Status: %s", readDatabasesResponse.Status()),
errMsg,
)
return
}
Expand Down Expand Up @@ -307,9 +323,17 @@ func (r *DatabaseResource) Update(ctx context.Context, req resource.UpdateReques
}

if updateDatabaseResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(updateDatabaseResponse, updateDatabaseResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error updating database",
fmt.Sprintf("Status: %s", updateDatabaseResponse.Status()),
errMsg,
)
return
}
Expand Down Expand Up @@ -351,9 +375,17 @@ func (r *DatabaseResource) Delete(ctx context.Context, req resource.DeleteReques
}

if deleteDatabasesResponse.StatusCode() != 204 {
errMsg, err := formatErrorResponse(deleteDatabasesResponse, deleteDatabasesResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error deleting database",
fmt.Sprintf("Status: %s", deleteDatabasesResponse.Status()),
errMsg,
)
return
}
Expand Down
16 changes: 12 additions & 4 deletions internal/provider/databases_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,24 @@ func (d *DatabasesDataSource) Read(ctx context.Context, req datasource.ReadReque
readDatabasesResponse, err := d.client.GetClusterDatabasesWithResponse(ctx, d.accountID, d.clusterID)
if err != nil {
resp.Diagnostics.AddError(
"Error getting Databases",
"Error getting databases",
err.Error(),
)
return
}

if readDatabasesResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(readDatabasesResponse, readDatabasesResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error getting Databases",
fmt.Sprintf("Status: %s", readDatabasesResponse.Status()),
"Error getting databases",
errMsg,
)
return
}
Expand All @@ -143,7 +151,7 @@ func (d *DatabasesDataSource) Read(ctx context.Context, req datasource.ReadReque
partitionTemplate, err := getPartitionTemplate(database.PartitionTemplate)
if err != nil {
resp.Diagnostics.AddError(
"Error getting Databases",
"Error getting databases",
err.Error(),
)
return
Expand Down
10 changes: 9 additions & 1 deletion internal/provider/token_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,17 @@ func (d *TokenDataSource) Read(ctx context.Context, req datasource.ReadRequest,
}

if readTokenResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(readTokenResponse, readTokenResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error getting token",
fmt.Sprintf("Status: %s", readTokenResponse.Status()),
errMsg,
)
return
}
Expand Down
40 changes: 36 additions & 4 deletions internal/provider/token_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,17 @@ func (r *TokenResource) Create(ctx context.Context, req resource.CreateRequest,
}

if createTokenResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(createTokenResponse, createTokenResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error creating token",
fmt.Sprintf("Status: %s", createTokenResponse.Status()),
errMsg,
)
return
}
Expand Down Expand Up @@ -198,9 +206,17 @@ func (r *TokenResource) Read(ctx context.Context, req resource.ReadRequest, resp
}

if readTokenResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(readTokenResponse, readTokenResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error getting token",
fmt.Sprintf("Status: %s", readTokenResponse.Status()),
errMsg,
)
return
}
Expand Down Expand Up @@ -269,9 +285,17 @@ func (r *TokenResource) Update(ctx context.Context, req resource.UpdateRequest,
}

if updateTokenResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(updateTokenResponse, updateTokenResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error updating token",
fmt.Sprintf("Status: %s", updateTokenResponse.Status()),
errMsg,
)
return
}
Expand Down Expand Up @@ -323,9 +347,17 @@ func (r *TokenResource) Delete(ctx context.Context, req resource.DeleteRequest,
}

if deleteTokenResponse.StatusCode() != 204 {
errMsg, err := formatErrorResponse(deleteTokenResponse, deleteTokenResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error deleting token",
fmt.Sprintf("Status: %s", deleteTokenResponse.Status()),
errMsg,
)
return
}
Expand Down
16 changes: 12 additions & 4 deletions internal/provider/tokens_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (d *TokensDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

readTokens, err := d.client.GetDatabaseTokensWithResponse(ctx, d.accountID, d.clusterID)
readTokensResponse, err := d.client.GetDatabaseTokensWithResponse(ctx, d.accountID, d.clusterID)
if err != nil {
resp.Diagnostics.AddError(
"Error getting tokens",
Expand All @@ -136,16 +136,24 @@ func (d *TokensDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

if readTokens.StatusCode() != 200 {
if readTokensResponse.StatusCode() != 200 {
errMsg, err := formatErrorResponse(readTokensResponse, readTokensResponse.StatusCode())
if err != nil {
resp.Diagnostics.AddError(
"Error formatting error response",
err.Error(),
)
return
}
resp.Diagnostics.AddError(
"Error getting tokens",
fmt.Sprintf("Status: %s", readTokens.Status()),
errMsg,
)
return
}

// Map response body to model
for _, token := range *readTokens.JSON200 {
for _, token := range *readTokensResponse.JSON200 {
tokenState := TokenModel{
AccountId: types.StringValue(token.AccountId.String()),
CreatedAt: types.StringValue(token.CreatedAt.String()),
Expand Down
26 changes: 26 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package provider

import (
"fmt"
"reflect"
"strconv"

"github.com/komminarlabs/influxdb3"
)

// formatErrorResponse formats the error response from the InfluxDB API
func formatErrorResponse(rsp interface{}, statusCode int) (string, error) {
v := reflect.ValueOf(rsp)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}

fieldName := "JSON" + strconv.Itoa(statusCode)
field := v.FieldByName(fieldName)
if !field.IsValid() {
return "", fmt.Errorf("field %s not found", fieldName)
}

errorDetail := field.Interface().(*influxdb3.Error)
return fmt.Sprintf("HTTP Status Code: %d\nError Code: %d\nError Message: %s\n", statusCode, errorDetail.Code, errorDetail.Message), nil
}

0 comments on commit 9992292

Please sign in to comment.