Fix to parse API error
This commit is contained in:
parent
fe02b9e6af
commit
aa0f9563ac
4 changed files with 21 additions and 7 deletions
17
helper.go
17
helper.go
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue