fix: proper error handling
This commit is contained in:
parent
6f40f9625f
commit
31aa25a8ee
12
libcapsul.go
12
libcapsul.go
|
@ -47,12 +47,12 @@ func (c CapsulClient) Create(capsulName, capsulType, capsulImage, capsulSSHKey s
|
|||
|
||||
payload, err := json.Marshal(values)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
return CapsulCreateResponse{}, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", capsulCreateURL, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
return CapsulCreateResponse{}, err
|
||||
}
|
||||
|
||||
req.Header = http.Header{
|
||||
|
@ -62,21 +62,21 @@ func (c CapsulClient) Create(capsulName, capsulType, capsulImage, capsulSSHKey s
|
|||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
return CapsulCreateResponse{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return CapsulCreateResponse{}, err
|
||||
}
|
||||
logrus.Fatal(string(body))
|
||||
return CapsulCreateResponse{}, fmt.Errorf(string(body))
|
||||
}
|
||||
|
||||
var resp CapsulCreateResponse
|
||||
if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
|
||||
logrus.Fatal(err)
|
||||
return CapsulCreateResponse{}, err
|
||||
}
|
||||
|
||||
logrus.Debugf("capsul created with ID: '%s'", resp.ID)
|
||||
|
|
Loading…
Reference in New Issue