Skip to content

Commit 0fd1a96

Browse files
committed
feat(client): Decode and return body of 404 errors if present.
1 parent c243c66 commit 0fd1a96

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

v3/api/client.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,33 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
403403
resp.StatusCode,
404404
stringMessage,
405405
)
406+
407+
var errorMessage map[string]interface{} // Decode JSON body to handle issue
408+
err = json.NewDecoder(resp.Body).Decode(&errorMessage)
409+
410+
if err != nil {
411+
_, derr := httputil.DumpResponse(resp, true)
412+
if derr != nil {
413+
log.Printf(
414+
"[ERROR] Error dumping response body for '%s' request to '%s': %s",
415+
request.Method,
416+
request.Endpoint,
417+
derr,
418+
)
419+
return nil, derr
420+
}
421+
uerr := errors.New(
422+
fmt.Sprintf(
423+
"%d - Unknown error connecting to Keyfactor %s, please check your connection.",
424+
resp.StatusCode,
425+
endpoint,
426+
),
427+
)
428+
log.Printf("[ERROR] %s", uerr)
429+
return nil, uerr
430+
}
431+
432+
stringMessage = fmt.Sprintf("%v", errorMessage["Message"])
406433
return nil, errors.New(stringMessage)
407434
} else if resp.StatusCode == http.StatusUnauthorized {
408435
dmp, derr := httputil.DumpResponse(resp, true)

0 commit comments

Comments
 (0)