Skip to content

Commit

Permalink
rework Error checking (#478)
Browse files Browse the repository at this point in the history
* rework Error checking

* test updates to API error (#479)

* add test for IsOpsLevelApiError

---------

Co-authored-by: David Bloss <[email protected]>
  • Loading branch information
rocktavious and davidbloss authored Oct 15, 2024
1 parent 45ce5c0 commit 0d08316
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
40 changes: 34 additions & 6 deletions clientGQL_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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":{} }`
Expand All @@ -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":{} }`
Expand All @@ -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))
}
18 changes: 8 additions & 10 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d08316

Please sign in to comment.