Handle json parsing error with masto API, return error with getBlog

This commit is contained in:
Romain de Laage 2021-03-05 10:46:31 +01:00
parent b6b7fd1cb5
commit d95b1bbccc
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
2 changed files with 41 additions and 12 deletions

View File

@ -25,34 +25,39 @@ import (
"fmt" "fmt"
) )
func getBlog(baseURL, account string) []Blog { func getBlog(baseURL, account string) ([]Blog, error) {
if baseURL == "" || account == "" { if baseURL == "" || account == "" {
log.Println("baseURL or account is empty") log.Println("baseURL or account is empty")
return nil return nil, fmt.Errorf("BaseURL or account is empty")
} }
resp, err := http.Get(baseURL + "/api/v1/accounts/" + account + "/statuses?exclude_reblogs=true&exclude_replies=true") resp, err := http.Get(baseURL + "/api/v1/accounts/" + account + "/statuses?exclude_reblogs=true&exclude_replies=true")
if err != nil { if err != nil {
log.Println("Mastodon API request: %s", err) log.Println("Mastodon API request: %s", err)
return nil return nil, fmt.Errorf("Failed to request Mastodon API")
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
log.Println("Mastodon API response: %s", resp.Status) log.Println("Mastodon API response: %s", resp.Status)
return nil return nil, fmt.Errorf("Mastodon instance failed")
} }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Println("Mastodon response body: %s", err) log.Println("Mastodon response body: %s", err)
return nil, fmt.Errorf("Failed to read Mastodon response body")
} }
var blogs []Blog var blogs []Blog
json.Unmarshal(body, &blogs) err = json.Unmarshal(body, &blogs)
if err != nil {
log.Println("Mastodon response %s", err)
return nil, fmt.Errorf("Failed to parse Mastodon response")
}
return blogs return blogs, nil
} }
func getAccount(baseURL, accountId string) (Account, error) { func getAccount(baseURL, accountId string) (Account, error) {
@ -81,7 +86,11 @@ func getAccount(baseURL, accountId string) (Account, error) {
} }
var account Account var account Account
json.Unmarshal(body, &account) err = json.Unmarshal(body, &account)
if err != nil {
log.Println("Mastodon response %s", err)
return Account{}, fmt.Errorf("Failed to parse response")
}
return account, nil return account, nil
} }
@ -112,7 +121,11 @@ func getToot(baseURL, tootId string) (Blog, error) {
} }
var toot Blog var toot Blog
json.Unmarshal(body, &toot) err = json.Unmarshal(body, &toot)
if err != nil {
log.Println("Mastodon response %s", err)
return Blog{}, fmt.Errorf("Failed to parse response")
}
return toot, nil return toot, nil
} }
@ -143,7 +156,11 @@ func getThread(baseURL, tootId string) (Thread, error) {
} }
var thread Thread var thread Thread
json.Unmarshal(body, &thread) err = json.Unmarshal(body, &thread)
if err != nil {
log.Println("Mastodon response %s", err)
return Thread{}, fmt.Errorf("Failed to parse response")
}
return thread, nil return thread, nil
} }
@ -174,7 +191,11 @@ func getTag(baseURL, tag string) ([]Blog, error) {
} }
var blogs []Blog var blogs []Blog
json.Unmarshal(body, &blogs) err = json.Unmarshal(body, &blogs)
if err != nil {
log.Println("Mastodon response: %s", err)
return nil, fmt.Errorf("Failed to parse response")
}
return blogs, nil return blogs, nil
} }

View File

@ -214,9 +214,17 @@ This capsule use %s Mastodon instance.
func printProfile(conn *tls.Conn, baseURL, profileID string) { func printProfile(conn *tls.Conn, baseURL, profileID string) {
account, err := getAccount(baseURL, profileID) account, err := getAccount(baseURL, profileID)
blogs := getBlog(baseURL, profileID) if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
if err != nil || blogs == nil { blogs, err := getBlog(baseURL, profileID)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n") _, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil { if err != nil {
log.Println("handleConn: %s", err) log.Println("handleConn: %s", err)