From b171259ddeee66f5ddd37e308c4b80fca47e6524 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Tue, 15 Dec 2015 17:51:48 +0100 Subject: [PATCH] pkg: plugins: fix and better handle errors Signed-off-by: Antonio Murdaca --- plugins/client.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/plugins/client.go b/plugins/client.go index 392543b..8990f88 100644 --- a/plugins/client.go +++ b/plugins/client.go @@ -131,11 +131,26 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool) } if resp.StatusCode != http.StatusOK { - remoteErr, err := ioutil.ReadAll(resp.Body) + b, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil, &remoteError{err.Error(), serviceMethod} + return nil, &remoteError{method: serviceMethod, err: err.Error()} } - return nil, &remoteError{string(remoteErr), serviceMethod} + + // Plugins' Response(s) should have an Err field indicating what went + // wrong. Try to unmarshal into ResponseErr. Otherwise fallback to just + // return the string(body) + type responseErr struct { + Err string + } + remoteErr := responseErr{} + if err := json.Unmarshal(b, &remoteErr); err != nil { + return nil, &remoteError{method: serviceMethod, err: err.Error()} + } + if remoteErr.Err != "" { + return nil, &remoteError{method: serviceMethod, err: remoteErr.Err} + } + // old way... + return nil, &remoteError{method: serviceMethod, err: string(b)} } return resp.Body, nil }