api: return error when `LicenseGet` status is not `200` (#11644)

This commit is contained in:
Luiz Aoqui 2021-12-14 19:47:09 -05:00 committed by GitHub
parent 235a778a56
commit 05bb65779c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

3
.changelog/11644.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
api: Improve error message returned by `Operator.LicenseGet`
```

View File

@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
@ -338,6 +339,11 @@ func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, *QueryMeta, erro
return nil, nil, errors.New("Nomad Enterprise only endpoint")
}
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
return nil, nil, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, body)
}
err = json.NewDecoder(resp.Body).Decode(&reply)
if err != nil {
return nil, nil, err

28
api/operator_ent_test.go Normal file
View File

@ -0,0 +1,28 @@
//go:build ent
// +build ent
package api
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestOperator_LicenseGet(t *testing.T) {
t.Parallel()
c, s, _ := makeACLClient(t, nil, nil)
defer s.Stop()
operator := c.Operator()
// Make authenticated request.
_, _, err := operator.LicenseGet(nil)
require.NoError(t, err)
// Make unauthenticated request.
c.SetSecretID("")
_, _, err = operator.LicenseGet(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "403")
}