Skip to content

Commit

Permalink
Merge pull request #71 from flovouin/fix/log-error-api-invalid-response
Browse files Browse the repository at this point in the history
🥅 Catch and return an error when the API returns a response that could not be parsed
  • Loading branch information
flovouin authored Sep 22, 2024
2 parents ecc0001 + 5bc152b commit 5c2ce10
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
BUG FIXES:

- Catch and return an error when the API returns a response that could not be parsed.

## 0.8.0 (2024-09-08)

BREAKING CHANGES:
Expand Down
9 changes: 9 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func checkMetabaseResponse(r metabase.MetabaseResponse, err error, statusCodes [
}
}

if r.HasExpectedStatusWithoutExpectedBody() {
return diag.Diagnostics{
diag.NewErrorDiagnostic(
fmt.Sprintf("Unexpected response while calling the Metabase API for operation '%s'.", operation),
fmt.Sprintf("Status code: %d, failed to parse body: %s", r.StatusCode(), r.BodyString()),
),
}
}

for _, s := range statusCodes {
if r.StatusCode() == s {
return diag.Diagnostics{}
Expand Down
117 changes: 117 additions & 0 deletions metabase/metabase_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,120 +5,237 @@ package metabase
type MetabaseResponse interface {
StatusCode() int
BodyString() string
HasExpectedStatusWithoutExpectedBody() bool
}

func (r *CreateCardResponse) BodyString() string {
return string(r.Body)
}

func (r *CreateCardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetCardResponse) BodyString() string {
return string(r.Body)
}

func (r *GetCardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdateCardResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdateCardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetCollectionPermissionsGraphResponse) BodyString() string {
return string(r.Body)
}

func (r *GetCollectionPermissionsGraphResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *ReplaceCollectionPermissionsGraphResponse) BodyString() string {
return string(r.Body)
}

func (r *ReplaceCollectionPermissionsGraphResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *CreateCollectionResponse) BodyString() string {
return string(r.Body)
}

func (r *CreateCollectionResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetCollectionResponse) BodyString() string {
return string(r.Body)
}

func (r *GetCollectionResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdateCollectionResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdateCollectionResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *ListCollectionItemsResponse) BodyString() string {
return string(r.Body)
}

func (r *ListCollectionItemsResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *CreateDashboardResponse) BodyString() string {
return string(r.Body)
}

func (r *CreateDashboardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetDashboardResponse) BodyString() string {
return string(r.Body)
}

func (r *GetDashboardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdateDashboardResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdateDashboardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *DeleteDashboardResponse) BodyString() string {
return string(r.Body)
}

func (r *DeleteDashboardResponse) HasExpectedStatusWithoutExpectedBody() bool {
return false
}

func (r *CreateDatabaseResponse) BodyString() string {
return string(r.Body)
}

func (r *CreateDatabaseResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetDatabaseResponse) BodyString() string {
return string(r.Body)
}

func (r *GetDatabaseResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdateDatabaseResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdateDatabaseResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *DeleteDatabaseResponse) BodyString() string {
return string(r.Body)
}

func (r *DeleteDatabaseResponse) HasExpectedStatusWithoutExpectedBody() bool {
return false
}

func (r *GetPermissionsGraphResponse) BodyString() string {
return string(r.Body)
}

func (r *GetPermissionsGraphResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *ReplacePermissionsGraphResponse) BodyString() string {
return string(r.Body)
}

func (r *ReplacePermissionsGraphResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *CreatePermissionsGroupResponse) BodyString() string {
return string(r.Body)
}

func (r *CreatePermissionsGroupResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetPermissionsGroupResponse) BodyString() string {
return string(r.Body)
}

func (r *GetPermissionsGroupResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdatePermissionsGroupResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdatePermissionsGroupResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *DeletePermissionsGroupResponse) BodyString() string {
return string(r.Body)
}

func (r *DeletePermissionsGroupResponse) HasExpectedStatusWithoutExpectedBody() bool {
return false
}

func (r *CreateSessionResponse) BodyString() string {
return string(r.Body)
}

func (r *CreateSessionResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *ListTablesResponse) BodyString() string {
return string(r.Body)
}

func (r *ListTablesResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetTableMetadataResponse) BodyString() string {
return string(r.Body)
}

func (r *GetTableMetadataResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdateTableResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdateTableResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *GetFieldResponse) BodyString() string {
return string(r.Body)
}

func (r *GetFieldResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

func (r *UpdateFieldResponse) BodyString() string {
return string(r.Body)
}

func (r *UpdateFieldResponse) HasExpectedStatusWithoutExpectedBody() bool {
return r.StatusCode() == 200 && r.JSON200 == nil
}

0 comments on commit 5c2ce10

Please sign in to comment.