Skip to content

Commit 7d84772

Browse files
Return body on error
1 parent b2fd22b commit 7d84772

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

pkg/diambra/client/client.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import (
77
"net/http"
88
"net/url"
99
"os"
10+
"strings"
1011

1112
"github.com/go-kit/log"
1213
"github.com/go-kit/log/level"
1314
)
1415

15-
var (
16-
ErrForbidden = errors.New("Forbidden")
17-
)
16+
type Error struct {
17+
error
18+
}
19+
20+
type ErrForbidden Error
1821

1922
// FIXME: Replace this with oapi generated code
2023

@@ -32,7 +35,7 @@ func readCredentials(credPath string) (string, error) {
3235
if err != nil {
3336
return "", fmt.Errorf("can't read credentials file %s: %w", credPath, err)
3437
}
35-
return string(b), nil
38+
return strings.TrimSpace(string(b)), nil
3639
}
3740

3841
func NewClient(logger log.Logger, credPath string) (*Client, error) {
@@ -77,7 +80,14 @@ func (c *Client) Request(method string, path string, body io.Reader, authenticat
7780
return nil, err
7881
}
7982
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
80-
return nil, ErrForbidden
83+
// read the body for error
84+
85+
rb, err := io.ReadAll(resp.Body)
86+
if err != nil {
87+
return nil, &ErrForbidden{fmt.Errorf("unauthorized; couldn't read body: %w", err)}
88+
}
89+
_ = resp.Body.Close()
90+
return nil, ErrForbidden{errors.New(string(rb))}
8191
}
8292
return resp, nil
8393
}

pkg/diambra/credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func EnsureCredentials(logger log.Logger, credPath string) error {
7676
return nil
7777
}
7878

79-
if err == client.ErrForbidden {
79+
if _, ok := err.(client.ErrForbidden); ok {
8080
if err := os.Remove(credPath); err != nil {
8181
return fmt.Errorf("couldn't remove credentials file %s: %w", credPath, err)
8282
}

0 commit comments

Comments
 (0)