Skip to content

Commit 135e1a6

Browse files
committed
Forward API errors to the data source.
This commit forwards the error returned by the API and displays it in order to help clients troubleshoot the issue.
1 parent ad7efac commit 135e1a6

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

internal/client/graph_client.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/clems4ever/go-graphkb/internal/knowledge"
1313
"github.com/clems4ever/go-graphkb/internal/schema"
1414
"github.com/clems4ever/go-graphkb/internal/utils"
15+
"github.com/sirupsen/logrus"
1516
)
1617

1718
// ErrTooManyRequests error representing too many requests to the API
@@ -64,7 +65,7 @@ func (gc *GraphClient) ReadCurrentGraph() (*knowledge.Graph, error) {
6465
if res.StatusCode == http.StatusUnauthorized {
6566
return nil, fmt.Errorf("Unauthorized access. Check your auth token")
6667
} else if res.StatusCode != http.StatusOK {
67-
return nil, fmt.Errorf("Expected status code 200 and got %d: %s", res.StatusCode, res.Status)
68+
return nil, handleUnexpectedResponse(res)
6869
}
6970

7071
b, err := ioutil.ReadAll(res.Body)
@@ -106,7 +107,7 @@ func (gc *GraphClient) UpdateSchema(sg schema.SchemaGraph) error {
106107
} else if res.StatusCode == http.StatusTooManyRequests {
107108
return ErrTooManyRequests
108109
} else if res.StatusCode != http.StatusOK {
109-
return fmt.Errorf("Expected status code 200 and got %d: %s", res.StatusCode, res.Status)
110+
return handleUnexpectedResponse(res)
110111
}
111112
return nil
112113
}
@@ -137,7 +138,7 @@ func (gc *GraphClient) InsertAssets(assets []knowledge.Asset) error {
137138
} else if res.StatusCode == http.StatusTooManyRequests {
138139
return ErrTooManyRequests
139140
} else if res.StatusCode != http.StatusOK {
140-
return fmt.Errorf("Expected status code 200 and got %d: %s", res.StatusCode, res.Status)
141+
return handleUnexpectedResponse(res)
141142
}
142143
return nil
143144
}
@@ -168,7 +169,7 @@ func (gc *GraphClient) DeleteAssets(assets []knowledge.Asset) error {
168169
} else if res.StatusCode == http.StatusTooManyRequests {
169170
return ErrTooManyRequests
170171
} else if res.StatusCode != http.StatusOK {
171-
return fmt.Errorf("Expected status code 200 and got %d: %s", res.StatusCode, res.Status)
172+
return handleUnexpectedResponse(res)
172173
}
173174
return nil
174175
}
@@ -199,7 +200,7 @@ func (gc *GraphClient) InsertRelations(relations []knowledge.Relation) error {
199200
} else if res.StatusCode == http.StatusTooManyRequests {
200201
return ErrTooManyRequests
201202
} else if res.StatusCode != http.StatusOK {
202-
return fmt.Errorf("Expected status code 200 and got %d: %s", res.StatusCode, res.Status)
203+
return handleUnexpectedResponse(res)
203204
}
204205
return nil
205206
}
@@ -230,7 +231,16 @@ func (gc *GraphClient) DeleteRelations(relations []knowledge.Relation) error {
230231
} else if res.StatusCode == http.StatusTooManyRequests {
231232
return ErrTooManyRequests
232233
} else if res.StatusCode != http.StatusOK {
233-
return fmt.Errorf("Expected status code 200 and got %d: %s", res.StatusCode, res.Status)
234+
return handleUnexpectedResponse(res)
234235
}
235236
return nil
236237
}
238+
239+
func handleUnexpectedResponse(res *http.Response) error {
240+
bodyBytes, err := ioutil.ReadAll(res.Body)
241+
if err != nil {
242+
logrus.Errorf("Unable to read error payload: %v", err)
243+
}
244+
bodyString := string(bodyBytes)
245+
return fmt.Errorf("Unexpected HTTP status %d with content %s: %s", res.StatusCode, res.Status, bodyString)
246+
}

internal/client/transaction.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func withRetryOnTooManyRequests(fn func() error, backoffFactor float64, maxRetri
6262
for {
6363
err := fn()
6464
if err != nil {
65+
logrus.Error(err)
6566
backoffTime := time.Duration(int(math.Pow(backoffFactor, float64(trials)))) * delay
6667
logrus.Info("Sleeping for %f seconds\n", backoffTime/time.Second)
6768
time.Sleep(backoffTime)

0 commit comments

Comments
 (0)