1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 21:45:43 +01:00
Files
glusterd2/pkg/restclient/errors.go
Prashanth Pai a19b8ae856 restclient: Refactor error response handling
* Simplify the do() method. Abstract creation of HTTP error.
* Save the last error response received from the server in the
  restclient.Client instance which can be accessed using the
  LastErrorResponse() method.

Signed-off-by: Prashanth Pai <ppai@redhat.com>
2018-07-30 09:13:19 +05:30

52 lines
1.1 KiB
Go

package restclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/gluster/glusterd2/pkg/api"
)
// HTTPErrorResponse is custom error that is returned when expected
// status code does not match with actual status code returned.
type HTTPErrorResponse struct {
Status int
Body string
Headers http.Header
}
func (e *HTTPErrorResponse) Error() string {
return fmt.Sprintf("Request failed. Status: %d\nResponse: %s", e.Status, e.Body)
}
func newHTTPErrorResponse(resp *http.Response) error {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var errResp api.ErrorResp
if err = json.Unmarshal(b, &errResp); err != nil {
return err
}
var buffer bytes.Buffer
// FIXME: The CLI should be doing this string processing.
for _, apiErr := range errResp.Errors {
switch api.ErrorCode(apiErr.Code) {
case api.ErrTxnStepFailed:
buffer.WriteString(fmt.Sprintf(
"Transaction step %s failed on peer %s with error: %s\n",
apiErr.Fields["step"], apiErr.Fields["peer-id"], apiErr.Fields["error"]))
default:
buffer.WriteString(apiErr.Message)
}
}
return fmt.Errorf("%s", buffer.String())
}