Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 atuhenticated request.
lgfa29 marked this conversation as resolved.
Show resolved Hide resolved
_, _, err := operator.LicenseGet(nil)
require.NoError(t, err)

// Make unatuhenticated request.
c.SetSecretID("")
_, _, err = operator.LicenseGet(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "403")
DerekStrickland marked this conversation as resolved.
Show resolved Hide resolved
}