crossle / hacker-news-mixin-bot

Post new hot stories from Hacker News to Mixin bot (Mixin ID: 7000100124)
MIT License
20 stars 5 forks source link

service 'news' crashed after started for a few seconds #2

Closed hxzqlh closed 5 years ago

hxzqlh commented 5 years ago
hxzdeMac-mini:~/workspace/golang/src/github.com/crossle/hacker-news-mixin-bot (master) $ ./hacker-news-mixin-bot -service news
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x42b6d48]

goroutine 1 [running]:
encoding/json.(*Decoder).refill(0xc42028a000, 0x4670680, 0xc420018101)
    /usr/local/go/src/encoding/json/stream.go:152 +0xe8
encoding/json.(*Decoder).readValue(0xc42028a000, 0x0, 0x0, 0x4013a68)
    /usr/local/go/src/encoding/json/stream.go:128 +0x275
encoding/json.(*Decoder).Decode(0xc42028a000, 0x45acec0, 0xc4200dcc40, 0x0, 0x0)
    /usr/local/go/src/encoding/json/stream.go:57 +0x78
github.com/crossle/hacker-news-mixin-bot/vendor/github.com/qube81/hackernews-api-go.GetStories(0x46a6e22, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/hxz/workspace/golang/src/github.com/crossle/hacker-news-mixin-bot/vendor/github.com/qube81/hackernews-api-go/api.go:67 +0x1e6
github.com/crossle/hacker-news-mixin-bot/services.getTopStoryId(0xc420251e28)
    /Users/hxz/workspace/golang/src/github.com/crossle/hacker-news-mixin-bot/services/news.go:31 +0x36
github.com/crossle/hacker-news-mixin-bot/services.(*NewsService).Run(0x4a2ede0, 0x49b12a0, 0xc4201b25d0, 0x49b12a0, 0xc4201b25d0)
    /Users/hxz/workspace/golang/src/github.com/crossle/hacker-news-mixin-bot/services/news.go:62 +0x34
github.com/crossle/hacker-news-mixin-bot/services.(*Hub).StartService(0xc4200dcc20, 0x7ffeefbffa79, 0x4, 0x0, 0x0)
    /Users/hxz/workspace/golang/src/github.com/crossle/hacker-news-mixin-bot/services/hub.go:37 +0x170
main.main()
    /Users/hxz/workspace/golang/src/github.com/crossle/hacker-news-mixin-bot/main.go:26 +0x114
hxzqlh commented 5 years ago
resp <nil> Get https://hacker-news.firebaseio.com/v0/topstories.json: dial tcp 35.201.97.85:443: i/o timeout

The error shouldn't be ignored in getJSON func, otherwise body will be nil:

// GetStories returns []int ids
func GetStories(target string) ([]int, error) {

    var stories Ids
    switch strings.ToLower(target) {
    case "new", "top", "job", "ask":
        body, _ := getJSON(apiEndpoint + target + "stories.json")
        err := json.NewDecoder(body).Decode(&stories)
        defer body.Close()

        if err != nil {
            return nil, err
        }

    }
    return stories, nil
}
crossle commented 5 years ago

Should process the error when HTTP failed, fixed