2018-05-04 16:53:21 +00:00
|
|
|
package service
|
|
|
|
|
2018-05-09 23:54:23 +00:00
|
|
|
import (
|
2022-02-28 12:34:44 +00:00
|
|
|
"database/sql"
|
2018-05-09 23:54:23 +00:00
|
|
|
"encoding/json"
|
2022-02-14 16:43:34 +00:00
|
|
|
"errors"
|
2018-05-09 23:54:23 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2022-02-14 16:43:34 +00:00
|
|
|
var (
|
|
|
|
ErrUnauthenticated = errors.New("unauthenticated, or invalid token")
|
2022-06-01 23:05:05 +00:00
|
|
|
ErrMissingLicense = errors.New("missing or invalid license")
|
2022-02-14 16:43:34 +00:00
|
|
|
)
|
|
|
|
|
2018-05-04 16:53:21 +00:00
|
|
|
type SetupAlreadyErr interface {
|
|
|
|
SetupAlready() bool
|
|
|
|
Error() string
|
|
|
|
}
|
|
|
|
|
2018-05-08 02:07:00 +00:00
|
|
|
type setupAlreadyErr struct{}
|
2018-05-04 16:53:21 +00:00
|
|
|
|
|
|
|
func (e setupAlreadyErr) Error() string {
|
2021-01-28 15:57:32 +00:00
|
|
|
return "Fleet has already been setup"
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e setupAlreadyErr) SetupAlready() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotSetupErr interface {
|
|
|
|
NotSetup() bool
|
|
|
|
Error() string
|
|
|
|
}
|
|
|
|
|
2018-05-08 02:07:00 +00:00
|
|
|
type notSetupErr struct{}
|
2018-05-04 16:53:21 +00:00
|
|
|
|
|
|
|
func (e notSetupErr) Error() string {
|
2021-01-28 15:57:32 +00:00
|
|
|
return "The Fleet instance is not set up yet"
|
2018-05-04 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e notSetupErr) NotSetup() bool {
|
|
|
|
return true
|
|
|
|
}
|
2018-05-08 02:07:00 +00:00
|
|
|
|
|
|
|
type NotFoundErr interface {
|
|
|
|
NotFound() bool
|
|
|
|
Error() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type notFoundErr struct{}
|
|
|
|
|
|
|
|
func (e notFoundErr) Error() string {
|
|
|
|
return "The resource was not found"
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:35:03 +00:00
|
|
|
func (e notFoundErr) NotFound() bool {
|
2018-05-08 02:07:00 +00:00
|
|
|
return true
|
|
|
|
}
|
2018-05-09 23:54:23 +00:00
|
|
|
|
2022-02-28 12:34:44 +00:00
|
|
|
// Implement Is so that errors.Is(err, sql.ErrNoRows) returns true for an
|
|
|
|
// error of type *notFoundError, without having to wrap sql.ErrNoRows
|
|
|
|
// explicitly.
|
|
|
|
func (e notFoundErr) Is(other error) bool {
|
|
|
|
return other == sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:54:23 +00:00
|
|
|
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
|
|
|
|
}
|