api: return error when `LicenseGet` status is not `200` (#11644)
This commit is contained in:
parent
235a778a56
commit
05bb65779c
|
@ -0,0 +1,3 @@
|
|||
```release-note:improvement
|
||||
api: Improve error message returned by `Operator.LicenseGet`
|
||||
```
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
}
|
Loading…
Reference in New Issue