return error message from instance to the user

This commit is contained in:
Dan Anglin 2024-07-04 02:39:47 +01:00
parent e5eb2d72a8
commit b39150175c
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -123,12 +123,33 @@ func (g *Client) sendRequest(method string, url string, requestBody io.Reader, o
defer response.Body.Close() defer response.Body.Close()
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest { 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( return fmt.Errorf(
"did not receive an OK response from the GoToSocial server: got %d", "did not receive an OK response from the GoToSocial server: got %d",
response.StatusCode, 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(
"message received from the instance: (%d) %q",
response.StatusCode,
message.Error,
)
}
if object == nil { if object == nil {
return nil return nil
} }