-
-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
550e5af
commit f75989f
Showing
2 changed files
with
122 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package scraper | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/hasura/go-graphql-client" | ||
) | ||
|
||
type graphqlErrors []error | ||
|
||
func (e graphqlErrors) Error() string { | ||
b := strings.Builder{} | ||
for _, err := range e { | ||
_, _ = b.WriteString(err.Error()) | ||
} | ||
return b.String() | ||
} | ||
|
||
type graphqlError struct { | ||
err graphql.Error | ||
} | ||
|
||
func (e graphqlError) Error() string { | ||
unwrapped := e.err.Unwrap() | ||
if unwrapped != nil { | ||
var networkErr graphql.NetworkError | ||
if errors.As(unwrapped, &networkErr) { | ||
if networkErr.StatusCode() == 422 { | ||
return networkErr.Body() | ||
} | ||
} | ||
} | ||
return e.err.Error() | ||
} | ||
|
||
// convertGraphqlError converts a graphql.Error or graphql.Errors into an error with a useful message. | ||
// graphql.Error swallows important information, so we need to convert it to a more useful error type. | ||
func convertGraphqlError(err error) error { | ||
var gqlErrs graphql.Errors | ||
if errors.As(err, &gqlErrs) { | ||
ret := make(graphqlErrors, len(gqlErrs)) | ||
for i, e := range gqlErrs { | ||
ret[i] = convertGraphqlError(e) | ||
} | ||
return ret | ||
} | ||
|
||
var gqlErr graphql.Error | ||
if errors.As(err, &gqlErr) { | ||
return graphqlError{gqlErr} | ||
} | ||
|
||
return err | ||
} |
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