diff --git a/mastoUtil.go b/mastoUtil.go index 21a1310..cec8cf9 100644 --- a/mastoUtil.go +++ b/mastoUtil.go @@ -25,34 +25,39 @@ import ( "fmt" ) -func getBlog(baseURL, account string) []Blog { +func getBlog(baseURL, account string) ([]Blog, error) { if baseURL == "" || account == "" { 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") if err != nil { log.Println("Mastodon API request: %s", err) - return nil + return nil, fmt.Errorf("Failed to request Mastodon API") } defer resp.Body.Close() if resp.StatusCode != 200 { log.Println("Mastodon API response: %s", resp.Status) - return nil + return nil, fmt.Errorf("Mastodon instance failed") } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("Mastodon response body: %s", err) + return nil, fmt.Errorf("Failed to read Mastodon response body") } 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) { @@ -81,7 +86,11 @@ func getAccount(baseURL, accountId string) (Account, error) { } 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 } @@ -112,7 +121,11 @@ func getToot(baseURL, tootId string) (Blog, error) { } 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 } @@ -143,7 +156,11 @@ func getThread(baseURL, tootId string) (Thread, error) { } 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 } @@ -174,7 +191,11 @@ func getTag(baseURL, tag string) ([]Blog, error) { } 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 } diff --git a/server.go b/server.go index 514618f..a901697 100644 --- a/server.go +++ b/server.go @@ -214,9 +214,17 @@ This capsule use %s Mastodon instance. func printProfile(conn *tls.Conn, baseURL, profileID string) { 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") if err != nil { log.Println("handleConn: %s", err)