Merge pull request #5 from roobre/master
authorMikael Berthe <mikael@lilotux.net>
Sat, 20 Apr 2019 20:02:41 +0200
changeset 249 3ba735776c8a
parent 247 17e32e951670 (current diff)
parent 248 f287d505dfda (diff)
child 250 f2c8ab41725b
child 252 e217b238546e
Merge pull request #5 from roobre/master Try to parse error string returned by server before falling back to generic message committer: GitHub <noreply@github.com>
--- a/api.go	Sun Jan 13 13:25:58 2019 +0100
+++ b/api.go	Sat Apr 20 20:02:41 2019 +0200
@@ -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.