Enhance error printing

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-07-23 22:43:23 -04:00
parent 6200b6c667
commit d770da7414
3 changed files with 21 additions and 5 deletions

View File

@ -94,11 +94,11 @@ var stackDeployCmd = &cobra.Command{
common.CheckError(deploymentErr)
default:
// Something else happened
log.Fatalln(selectionErr)
common.CheckError(stackRetrievalErr)
}
default:
// Something else happened
log.Fatalln(stackRetrievalErr)
common.CheckError(stackRetrievalErr)
}
},
}

View File

@ -1,7 +1,11 @@
package common
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
@ -9,17 +13,25 @@ import (
// CheckError checks if an error occurred (it's not nil)
func CheckError(err error) {
if err != nil {
log.Fatalln(err)
log.Fatalln(fmt.Sprintf("Error: %s", err.Error()))
}
}
func CheckResponseForErrors(resp *http.Response) error {
if 300 <= resp.StatusCode {
// Guess it's a GenericError
respBody := GenericError{}
err := json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
// It's not a GenericError
bodyBytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
}
resp.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
return errors.New(string(bodyBytes))
}
return &respBody
}
return nil

View File

@ -70,7 +70,11 @@ type GenericError struct {
}
func (e *GenericError) Error() string {
if e.Details != "" {
return fmt.Sprintf("%s: %s", e.Err, e.Details)
} else {
return fmt.Sprintf("%s", e.Err)
}
}
type AuthenticateUserRequest struct {