psu/common/customerrors.go
Juan Carlos Mejías Rodríguez d770da7414 Enhance error printing
2019-07-23 22:44:29 -04:00

39 lines
766 B
Go

package common
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// CheckError checks if an error occurred (it's not nil)
func CheckError(err error) {
if err != nil {
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
}