mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 01:18:42 +00:00
Improve client error messages with unexpected server errors (#1776)
This commit is contained in:
parent
d7b0abd782
commit
4dfc1ca25e
6 changed files with 104 additions and 21 deletions
|
|
@ -1,5 +1,10 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
type SetupAlreadyErr interface {
|
||||
SetupAlready() bool
|
||||
Error() string
|
||||
|
|
@ -59,3 +64,25 @@ func (e notFoundErr) Error() string {
|
|||
func (n notFoundErr) NotFound() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type serverError struct {
|
||||
Message string `json:"message"`
|
||||
Errors []struct {
|
||||
Name string `json:"name"`
|
||||
Reason string `json:"reason"`
|
||||
} `json:"errors"`
|
||||
}
|
||||
|
||||
func extractServerErrorText(body io.Reader) string {
|
||||
var serverErr serverError
|
||||
if err := json.NewDecoder(body).Decode(&serverErr); err != nil {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
errText := serverErr.Message
|
||||
if len(serverErr.Errors) > 0 {
|
||||
errText += ": " + serverErr.Errors[0].Reason
|
||||
}
|
||||
|
||||
return errText
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ func (c *Client) ApplyLabels(specs []*kolide.LabelSpec) error {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("apply label spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"apply labels received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody applyLabelSpecsResponse
|
||||
|
|
@ -49,9 +53,12 @@ func (c *Client) GetLabel(name string) (*kolide.LabelSpec, error) {
|
|||
case http.StatusNotFound:
|
||||
return nil, notFoundErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("get label spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return nil, errors.Errorf(
|
||||
"get label received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody getLabelSpecResponse
|
||||
|
|
@ -76,7 +83,11 @@ func (c *Client) GetLabels() ([]*kolide.LabelSpec, error) {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("get label spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return nil, errors.Errorf(
|
||||
"get labels received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody getLabelSpecsResponse
|
||||
|
|
@ -105,9 +116,12 @@ func (c *Client) DeleteLabel(name string) error {
|
|||
case http.StatusNotFound:
|
||||
return notFoundErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("get label spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"delete label received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody deleteLabelResponse
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ func (c *Client) ApplyPacks(specs []*kolide.PackSpec) error {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("apply pack spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"apply packs received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody applyPackSpecsResponse
|
||||
|
|
@ -49,9 +53,12 @@ func (c *Client) GetPack(name string) (*kolide.PackSpec, error) {
|
|||
case http.StatusNotFound:
|
||||
return nil, notFoundErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("get pack spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return nil, errors.Errorf(
|
||||
"get pack received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody getPackSpecResponse
|
||||
|
|
@ -76,7 +83,11 @@ func (c *Client) GetPacks() ([]*kolide.PackSpec, error) {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("get pack spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return nil, errors.Errorf(
|
||||
"get packs received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody getPackSpecsResponse
|
||||
|
|
@ -105,9 +116,12 @@ func (c *Client) DeletePack(name string) error {
|
|||
case http.StatusNotFound:
|
||||
return notFoundErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("get pack spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"delete pack received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody deletePackResponse
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ func (c *Client) ApplyQueries(specs []*kolide.QuerySpec) error {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("apply query spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"apply queries received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody applyQuerySpecsResponse
|
||||
|
|
@ -49,9 +53,12 @@ func (c *Client) GetQuery(name string) (*kolide.QuerySpec, error) {
|
|||
case http.StatusNotFound:
|
||||
return nil, notFoundErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("get query spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return nil, errors.Errorf(
|
||||
"get query received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody getQuerySpecResponse
|
||||
|
|
@ -76,7 +83,11 @@ func (c *Client) GetQueries() ([]*kolide.QuerySpec, error) {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("get query spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return nil, errors.Errorf(
|
||||
"get queries received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody getQuerySpecsResponse
|
||||
|
|
@ -105,9 +116,12 @@ func (c *Client) DeleteQuery(name string) error {
|
|||
case http.StatusNotFound:
|
||||
return notFoundErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("get query spec got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"delete query received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody deleteQueryResponse
|
||||
|
|
|
|||
|
|
@ -27,9 +27,12 @@ func (c *Client) Login(email, password string) (string, error) {
|
|||
case http.StatusUnauthorized:
|
||||
return "", invalidLoginErr{}
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return "", errors.Errorf("login got HTTP %d, expected 200", response.StatusCode)
|
||||
return "", errors.Errorf(
|
||||
"login received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody loginResponse
|
||||
|
|
@ -54,7 +57,11 @@ func (c *Client) Logout() error {
|
|||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("logout got HTTP %d, expected 200", response.StatusCode)
|
||||
return errors.Errorf(
|
||||
"logout received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
var responseBody logoutResponse
|
||||
|
|
|
|||
|
|
@ -36,6 +36,13 @@ func (c *Client) Setup(email, password, org string) (string, error) {
|
|||
if response.StatusCode == http.StatusNotFound {
|
||||
return "", setupAlreadyErr{}
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return "", errors.Errorf(
|
||||
"setup received status %d %s",
|
||||
response.StatusCode,
|
||||
extractServerErrorText(response.Body),
|
||||
)
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return "", errors.Errorf("setup got HTTP %d, expected 200", response.StatusCode)
|
||||
|
|
|
|||
Loading…
Reference in a new issue