From 0d08316a465b5bfbd775852c18730b7b6c3d03a3 Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 15 Oct 2024 10:56:13 -0500 Subject: [PATCH] rework Error checking (#478) * rework Error checking * test updates to API error (#479) * add test for IsOpsLevelApiError --------- Co-authored-by: David Bloss --- clientGQL_test.go | 40 ++++++++++++++++++++++++++++++++++------ common.go | 18 ++++++++---------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/clientGQL_test.go b/clientGQL_test.go index 6fcb4427..326fe794 100644 --- a/clientGQL_test.go +++ b/clientGQL_test.go @@ -14,6 +14,7 @@ import ( "text/template" "github.com/Masterminds/sprig/v3" + "github.com/hasura/go-graphql-client" ol "github.com/opslevel/opslevel-go/v2024" "github.com/rocktavious/autopilot/v2023" @@ -177,7 +178,26 @@ func httpResponseByCode(statusCode int) autopilot.ResponseWriter { } } -func TestClientQueryHasBadHttpStatus(t *testing.T) { +func TestMissingTeamIsAnOpsLevelApiError(t *testing.T) { + testRequest := autopilot.NewTestRequest( + `query TeamGet($id:ID!){account{team(id: $id){alias,id,aliases,managedAliases,contacts{address,displayName,displayType,externalId,id,isDefault,type},htmlUrl,manager{id,email,htmlUrl,name,role},memberships{nodes{role,team{alias,id},user{id,email}},{{ template "pagination_request" }},totalCount},name,parentTeam{alias,id},responsibilities,tags{nodes{id,key,value},{{ template "pagination_request" }},totalCount}}}}`, + `{ {{ template "id1" }} }`, + `{"errors": [ + { + "message": "Team with id '{{ template "id1_string" }}' does not exist on this account", + "path": ["account", "team"], + "locations": [{"line": 1, "column": 32}] + } + ]}`, + ) + client := BestTestClient(t, "team/missing_team", testRequest) + // Act + _, err := client.GetTeam(id1) + // Assert + autopilot.Equals(t, true, ol.IsOpsLevelApiError(err)) +} + +func Test404ResponseNotAnOpsLevelApiError(t *testing.T) { // Arrange headers := map[string]string{"x": "x"} request := `{ "query": "{account{id}}", "variables":{} }` @@ -202,10 +222,10 @@ func TestClientQueryHasBadHttpStatus(t *testing.T) { // Act err := client.Query(&q, v) // Assert - autopilot.Equals(t, true, ol.HasBadHttpStatus(err)) + autopilot.Equals(t, false, ol.IsOpsLevelApiError(err)) } -func TestClientQueryHasOkHttpStatus(t *testing.T) { +func TestGenericHasuraErrorNotAnOpsLevelApiError(t *testing.T) { // Arrange headers := map[string]string{"x": "x"} request := `{ "query": "{account{id}}", "variables":{} }` @@ -230,7 +250,15 @@ func TestClientQueryHasOkHttpStatus(t *testing.T) { // Act err := client.Query(&q, v) // Assert - autopilot.Equals(t, false, ol.HasBadHttpStatus(err)) - autopilot.Equals(t, false, ol.HasBadHttpStatus(nil)) - autopilot.Equals(t, false, ol.HasBadHttpStatus(errors.New("asdf"))) + _, isHasuraError := err.(graphql.Errors) + autopilot.Equals(t, true, isHasuraError) + autopilot.Equals(t, false, ol.IsOpsLevelApiError(err)) +} + +func TestGenericErrorIsNotAnOpsLevelApiError(t *testing.T) { + autopilot.Equals(t, false, ol.IsOpsLevelApiError(errors.New("asdf"))) +} + +func TestNilErrorIsNotAnOpsLevelApiError(t *testing.T) { + autopilot.Equals(t, false, ol.IsOpsLevelApiError(nil)) } diff --git a/common.go b/common.go index c29be334..0607428d 100644 --- a/common.go +++ b/common.go @@ -81,16 +81,14 @@ func FormatErrors(errs []OpsLevelErrors) error { return allErrors } -// Checks if error has any HTTP status code, and if it's 300 or greater -func HasBadHttpStatus(err error) bool { - if hasuraErrs, ok := err.(graphql.Errors); ok { - for _, hasuraErr := range hasuraErrs { - netErr := hasuraErr.Unwrap() - if netErr, ok := netErr.(graphql.NetworkError); ok { - if netErr.StatusCode() >= 300 { - return true - } - } +// IsOpsLevelApiError checks if the error is returned by OpsLevel's API +func IsOpsLevelApiError(err error) bool { + if _, ok := err.(graphql.Errors); !ok { + return false + } + for _, hasuraErr := range err.(graphql.Errors) { + if len(hasuraErr.Path) > 0 { + return true } } return false