128 res, err := c.MakeRequest(req) |
128 res, err := c.MakeRequest(req) |
129 if err != nil { |
129 if err != nil { |
130 return nil, err |
130 return nil, err |
131 } |
131 } |
132 if res.StatusCode < 200 || res.StatusCode >= 300 { |
132 if res.StatusCode < 200 || res.StatusCode >= 300 { |
|
133 var errorText string |
|
134 // Try to unmarshal the returned error object for a description |
|
135 mastodonError := Error{} |
|
136 decodeErr := json.NewDecoder(res.Body).Decode(&mastodonError) |
|
137 if decodeErr != nil { |
|
138 // Decode unsuccessful, fallback to generic error based on response code |
|
139 errorText = http.StatusText(res.StatusCode) |
|
140 } else { |
|
141 errorText = mastodonError.Text |
|
142 } |
|
143 |
133 // Please note that the error string code is used by Search() |
144 // Please note that the error string code is used by Search() |
134 // to check the error cause. |
145 // to check the error cause. |
135 const errFormatString = "bad server status code (%d)" |
146 const errFormatString = "bad server status code (%d)" |
136 return nil, errors.Errorf(errFormatString+": %s", |
147 return nil, errors.Errorf(errFormatString+": %s", |
137 res.StatusCode, http.StatusText(res.StatusCode)) |
148 res.StatusCode, errorText) |
138 } |
149 } |
139 |
150 |
140 // Build Response object. |
151 // Build Response object. |
141 response, err := rest.BuildResponse(res) |
152 response, err := rest.BuildResponse(res) |
142 if err != nil { |
153 if err != nil { |