fix: proper error handling

This commit is contained in:
decentral1se 2021-10-20 17:17:04 +02:00
parent 6f40f9625f
commit 31aa25a8ee
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
1 changed files with 6 additions and 6 deletions

View File

@ -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)