Skip to content

Commit

Permalink
api: return error when LicenseGet status is not 200 (#11644)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Dec 15, 2021
1 parent 608cdfc commit 86e2dd7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11644.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: Improve error message returned by `Operator.LicenseGet`
```
6 changes: 6 additions & 0 deletions api/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions api/operator_ent_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 86e2dd7

Please sign in to comment.