# HG changeset patch # User Mikael Berthe # Date 1555783361 -7200 # Node ID 3ba735776c8a21b148fa254e0f969b930ce4efa0 # Parent 17e32e951670923a5210df2e2dc31f2f4a3a39b4# Parent f287d505dfda9daaf30955716d6c9b3721f4a2d3 Merge pull request #5 from roobre/master Try to parse error string returned by server before falling back to generic message committer: GitHub diff -r 17e32e951670 -r 3ba735776c8a api.go --- 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.