Fix to parse API error
parent
fe02b9e6af
commit
aa0f9563ac
3
apps.go
3
apps.go
|
@ -3,7 +3,6 @@ package mastodon
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
@ -64,7 +63,7 @@ func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("bad request: %v", resp.Status)
|
return nil, parseAPIError("bad request", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
var app Application
|
var app Application
|
||||||
|
|
17
helper.go
17
helper.go
|
@ -2,6 +2,9 @@ package mastodon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"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.
|
// String is a helper function to get the pointer value of a string.
|
||||||
func String(v string) *string { return &v }
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -131,7 +130,7 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("bad request: %v", resp.Status)
|
return parseAPIError("bad request", resp)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -175,7 +174,7 @@ func (c *Client) Authenticate(ctx context.Context, username, password string) er
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("bad authorization: %v", resp.Status)
|
return parseAPIError("bad authorization", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
res := struct {
|
res := struct {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -95,7 +94,7 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even
|
||||||
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||||
resp, err = c.Do(req)
|
resp, err = c.Do(req)
|
||||||
if resp != nil && resp.StatusCode != 200 {
|
if resp != nil && resp.StatusCode != 200 {
|
||||||
err = fmt.Errorf("bad request: %v", resp.Status)
|
err = parseAPIError("bad request", resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
Loading…
Reference in New Issue