mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
all: uninstall service if got invalid config from API
This commit is contained in:
committed by
Cuong Manh Le
parent
3a5c71514c
commit
53306235dc
@@ -3,13 +3,15 @@ package controld
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const resolverDataURL = "https://api.controld.com/utility"
|
||||
const (
|
||||
resolverDataURL = "https://api.controld.com/utility"
|
||||
InvalidConfigCode = 40401
|
||||
)
|
||||
|
||||
// ResolverConfig represents Control D resolver data.
|
||||
type ResolverConfig struct {
|
||||
@@ -24,12 +26,17 @@ type utilityResponse struct {
|
||||
} `json:"body"`
|
||||
}
|
||||
|
||||
type utilityErrorResponse struct {
|
||||
Error struct {
|
||||
type UtilityErrorResponse struct {
|
||||
ErrorField struct {
|
||||
Message string `json:"message"`
|
||||
Code int `json:"code"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func (u UtilityErrorResponse) Error() string {
|
||||
return u.ErrorField.Message
|
||||
}
|
||||
|
||||
type utilityRequest struct {
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
@@ -53,11 +60,11 @@ func FetchResolverConfig(uid string) (*ResolverConfig, error) {
|
||||
defer resp.Body.Close()
|
||||
d := json.NewDecoder(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
errResp := &utilityErrorResponse{}
|
||||
errResp := &UtilityErrorResponse{}
|
||||
if err := d.Decode(errResp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New(errResp.Error.Message)
|
||||
return nil, errResp
|
||||
}
|
||||
|
||||
ur := &utilityResponse{}
|
||||
|
||||
Reference in New Issue
Block a user