Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RequestError and GraphQLError types to indicate server failure and graphql failure. #95

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,53 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/url"
"regexp"
"runtime"
"strconv"
"strings"
"time"

lightspark "github.com/lightsparkdev/go-sdk"
)

// RequestError indicates that a request to the Lightspark API failed.
// It could be due to a service outage or a network error.
// The request should be retried if RequestError is returned with server errors (500-599).
type RequestError struct {
Message string
StatusCode int
}

func (e RequestError) Error() string {
return "lightspark request failed: " + strconv.Itoa(e.StatusCode) + ": " + e.Message
}

// GraphQLInternalError indicates there's a failure in the Lightspark API.
// It could be due to a bug on Ligthspark's side.
// The request can be retried, because the error might be transient.
type GraphQLInternalError struct {
Message string
}

func (e GraphQLInternalError) Error() string {
return "lightspark request failed: " + e.Message
}

// GraphQLError indicates the GraphQL request succeeded, but there's a user error.
// The request should not be retried, because the error is due to the user's input.
type GraphQLError struct {
Message string
Type string
}

func (e GraphQLError) Error() string {
return e.Type + ": " + e.Message
}

type Requester struct {
ApiTokenClientId string

Expand Down Expand Up @@ -118,6 +153,9 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
}

request, err := http.NewRequest("POST", serverUrl, bytes.NewBuffer(encodedPayload))
if err != nil {
return nil, err
}
request.SetBasicAuth(r.ApiTokenClientId, r.ApiTokenClientSecret)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("X-GraphQL-Operation", operationName)
Expand All @@ -133,6 +171,9 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
"v": 1,
"signature": base64.StdEncoding.EncodeToString(signature),
})
if err != nil {
return nil, err
}
request.Header.Add("X-Lightspark-Signing", bytes.NewBuffer(signaturePayloadBytes).String())
}

Expand All @@ -146,10 +187,14 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode > 299 {
return nil, errors.New("lightspark request failed: " + response.Status)
return nil, RequestError { Message: response.Status, StatusCode: response.StatusCode }
}

data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}

data, err := ioutil.ReadAll(response.Body)
var result map[string]interface{}
err = json.Unmarshal(data, &result)
if err != nil {
Expand All @@ -161,14 +206,14 @@ func (r *Requester) ExecuteGraphql(query string, variables map[string]interface{
errMap := err.(map[string]interface{})
errorMessage := errMap["message"].(string)
if errMap["extensions"] == nil {
return nil, errors.New(errorMessage)
return nil, GraphQLInternalError{Message: errorMessage}
}
extensions := errMap["extensions"].(map[string]interface{})
if extensions["error_name"] == nil {
return nil, errors.New(errorMessage)
return nil, GraphQLInternalError{Message: errorMessage}
}
errorName := extensions["error_name"].(string)
return nil, errors.New(errorName + " - " + errorMessage)
return nil, GraphQLError{Message: errorMessage, Type: errorName}
}

return result["data"].(map[string]interface{}), nil
Expand Down
Loading