fix(api): ensure getRecentGifs doesn't error when response is empty

Because the BLOB retrieved from the database can be empty, umarshalling
the data into JSON causes an error.

The API should simply return an empty list of there are no recent gifs.
This commit is contained in:
Pascal Precht 2023-02-07 12:29:22 +01:00 committed by r4bbit
parent dea21f440a
commit 41654bf49d
1 changed files with 7 additions and 4 deletions

View File

@ -131,12 +131,15 @@ func (api *API) GetRecentGifs() (recentGifs []Gif, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
recentGifs = make([]Gif, 0)
savedRecentGifs := []Gif{} savedRecentGifs := []Gif{}
if len(gifs) > 0 {
err = json.Unmarshal(gifs, &savedRecentGifs) err = json.Unmarshal(gifs, &savedRecentGifs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
recentGifs = savedRecentGifs recentGifs = savedRecentGifs
}
return recentGifs, nil return recentGifs, nil
} }