Fix to parse API error

This commit is contained in:
178inaba 2017-04-19 14:32:53 +09:00
parent fe02b9e6af
commit aa0f9563ac
4 changed files with 21 additions and 7 deletions

View file

@ -2,6 +2,9 @@ package mastodon
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
)
@ -36,3 +39,17 @@ func Base64Encode(file *os.File) (string, error) {
// String is a helper function to get the pointer value of a string.
func String(v string) *string { return &v }
func parseAPIError(prefix string, resp *http.Response) error {
errMsg := fmt.Sprintf("%s: %s", prefix, resp.Status)
var e struct {
Error string `json:"error"`
}
json.NewDecoder(resp.Body).Decode(&e)
if e.Error != "" {
errMsg = fmt.Sprintf("%s: %s", errMsg, e.Error)
}
return errors.New(errMsg)
}