@@ -12,6 +12,7 @@ import (
12
12
"github.com/clems4ever/go-graphkb/internal/knowledge"
13
13
"github.com/clems4ever/go-graphkb/internal/schema"
14
14
"github.com/clems4ever/go-graphkb/internal/utils"
15
+ "github.com/sirupsen/logrus"
15
16
)
16
17
17
18
// ErrTooManyRequests error representing too many requests to the API
@@ -64,7 +65,7 @@ func (gc *GraphClient) ReadCurrentGraph() (*knowledge.Graph, error) {
64
65
if res .StatusCode == http .StatusUnauthorized {
65
66
return nil , fmt .Errorf ("Unauthorized access. Check your auth token" )
66
67
} 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 )
68
69
}
69
70
70
71
b , err := ioutil .ReadAll (res .Body )
@@ -106,7 +107,7 @@ func (gc *GraphClient) UpdateSchema(sg schema.SchemaGraph) error {
106
107
} else if res .StatusCode == http .StatusTooManyRequests {
107
108
return ErrTooManyRequests
108
109
} 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 )
110
111
}
111
112
return nil
112
113
}
@@ -137,7 +138,7 @@ func (gc *GraphClient) InsertAssets(assets []knowledge.Asset) error {
137
138
} else if res .StatusCode == http .StatusTooManyRequests {
138
139
return ErrTooManyRequests
139
140
} 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 )
141
142
}
142
143
return nil
143
144
}
@@ -168,7 +169,7 @@ func (gc *GraphClient) DeleteAssets(assets []knowledge.Asset) error {
168
169
} else if res .StatusCode == http .StatusTooManyRequests {
169
170
return ErrTooManyRequests
170
171
} 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 )
172
173
}
173
174
return nil
174
175
}
@@ -199,7 +200,7 @@ func (gc *GraphClient) InsertRelations(relations []knowledge.Relation) error {
199
200
} else if res .StatusCode == http .StatusTooManyRequests {
200
201
return ErrTooManyRequests
201
202
} 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 )
203
204
}
204
205
return nil
205
206
}
@@ -230,7 +231,16 @@ func (gc *GraphClient) DeleteRelations(relations []knowledge.Relation) error {
230
231
} else if res .StatusCode == http .StatusTooManyRequests {
231
232
return ErrTooManyRequests
232
233
} 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 )
234
235
}
235
236
return nil
236
237
}
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
+ }
0 commit comments