Browse Source

client: Try to parse response Body on InternalServerError

Try to parse the response body from the server when an HTTP 500 is
returned (i.e. http.StatusInternalServerError) as it might contain
useful information. If successful, create a new error and append that
information to the returned error message. Otherwise just maintain the
same behavior
Alexandros Kosiaris 4 years ago
parent
commit
638643cda7
1 changed files with 9 additions and 2 deletions
  1. 9 2
      client/request.go

+ 9 - 2
client/request.go

@@ -104,8 +104,15 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
 		response.Body.Close()
 		response.Body.Close()
 		return nil, ErrForbidden
 		return nil, ErrForbidden
 	case http.StatusInternalServerError:
 	case http.StatusInternalServerError:
-		response.Body.Close()
-		return nil, ErrServerError
+		defer response.Body.Close()
+
+		var resp errorResponse
+		decoder := json.NewDecoder(response.Body)
+		// If we failed to decode, just return a generic ErrServerError
+		if err := decoder.Decode(&resp); err != nil {
+			return nil, ErrServerError
+		}
+		return nil, errors.New("miniflux: internal server error: " + resp.ErrorMessage)
 	case http.StatusNotFound:
 	case http.StatusNotFound:
 		response.Body.Close()
 		response.Body.Close()
 		return nil, ErrNotFound
 		return nil, ErrNotFound