Skip to content

Commit 9b84660

Browse files
committed
fix: added error message for 401 responses from Keyfactor and updated the http error catchall to handle JSON and non JSON responses.
1 parent 38569dc commit 9b84660

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

api/client.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"log"
1010
"net/http"
11+
"net/http/httputil"
1112
"net/url"
1213
"path"
1314
"strings"
@@ -138,7 +139,6 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
138139
log.Printf("[DEBUG] %s succeeded with response code %d", request.Method, resp.StatusCode)
139140
return resp, nil
140141
} else if resp.StatusCode == http.StatusNotFound {
141-
142142
var errorMessage interface{} // Decode JSON body to handle issue
143143
// First, try to serialize the response into an interface, but catch the exception.
144144
defer func() {
@@ -153,11 +153,24 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
153153
stringMessage = fmt.Sprintf("%v", errorMessage)
154154
log.Printf("[ERROR] Call to %s returned status %d. %s", keyfactorPath, resp.StatusCode, stringMessage)
155155
return nil, errors.New(stringMessage)
156+
} else if resp.StatusCode == http.StatusUnauthorized {
157+
_, derr := httputil.DumpResponse(resp, true)
158+
if derr != nil {
159+
return nil, derr
160+
}
161+
err = errors.New("401 - Unauthorized: Access is denied due to invalid credentials.")
162+
return nil, err
156163
} else {
157164
var errorMessage interface{} // Decode JSON body to handle issue
158165
err = json.NewDecoder(resp.Body).Decode(&errorMessage)
166+
159167
if err != nil {
160-
return nil, err
168+
_, derr := httputil.DumpResponse(resp, true)
169+
if derr != nil {
170+
return nil, derr
171+
}
172+
uerr := errors.New(fmt.Sprintf("%s - Unknown error connecting to Keyfactor %s, please check your connection.", resp.StatusCode, endpoint))
173+
return nil, uerr
161174
}
162175

163176
log.Printf("[DEBUG] Request failed with code %d and message %v", resp.StatusCode, errorMessage)

0 commit comments

Comments
 (0)