all: uninstall service if got invalid config from API

This commit is contained in:
Cuong Manh Le
2023-01-11 22:31:00 +07:00
committed by Cuong Manh Le
parent 3a5c71514c
commit 53306235dc
2 changed files with 28 additions and 8 deletions
+13 -6
View File
@@ -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{}