From b39150175c5c4eb325393df036c8b33ad763b025 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 4 Jul 2024 02:39:47 +0100 Subject: [PATCH] return error message from instance to the user --- internal/client/client.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/client/client.go b/internal/client/client.go index 620b957..7a5a43e 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -123,9 +123,30 @@ func (g *Client) sendRequest(method string, url string, requestBody io.Reader, o defer response.Body.Close() if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest { + message := struct { + Error string `json:"error"` + }{ + Error: "", + } + + if err := json.NewDecoder(response.Body).Decode(&message); err != nil { + return fmt.Errorf( + "did not receive an OK response from the GoToSocial server: got %d", + response.StatusCode, + ) + } + + if message.Error == "" { + return fmt.Errorf( + "did not receive an OK response from the GoToSocial server: got %d", + response.StatusCode, + ) + } + return fmt.Errorf( - "did not receive an OK response from the GoToSocial server: got %d", + "message received from the instance: (%d) %q", response.StatusCode, + message.Error, ) }