vendor/github.com/McKael/madon/v2/api.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
--- a/vendor/github.com/McKael/madon/v2/api.go	Wed Sep 18 19:17:42 2019 +0200
+++ b/vendor/github.com/McKael/madon/v2/api.go	Sun Feb 16 18:54:01 2020 +0100
@@ -130,11 +130,22 @@
 		return nil, err
 	}
 	if res.StatusCode < 200 || res.StatusCode >= 300 {
+		var errorText string
+		// Try to unmarshal the returned error object for a description
+		mastodonError := Error{}
+		decodeErr := json.NewDecoder(res.Body).Decode(&mastodonError)
+		if decodeErr != nil {
+			// Decode unsuccessful, fallback to generic error based on response code
+			errorText = http.StatusText(res.StatusCode)
+		} else {
+			errorText = mastodonError.Text
+		}
+
 		// Please note that the error string code is used by Search()
 		// to check the error cause.
 		const errFormatString = "bad server status code (%d)"
 		return nil, errors.Errorf(errFormatString+": %s",
-			res.StatusCode, http.StatusText(res.StatusCode))
+			res.StatusCode, errorText)
 	}
 
 	// Build Response object.