Fix panic on scryfall error

This commit is contained in:
Artyom Belousov 2022-03-22 21:19:29 +03:00
parent ca0af01268
commit c6b0a150f1
2 changed files with 20 additions and 5 deletions

View file

@ -2,11 +2,12 @@ package cardsinfo
import (
"encoding/json"
"gitlab.com/flygrounder/go-mtg-vk/internal/dicttranslate"
"io/ioutil"
"net/http"
"net/url"
"strings"
"gitlab.com/flygrounder/go-mtg-vk/internal/dicttranslate"
)
const scryfallUrl = "https://api.scryfall.com"
@ -38,13 +39,16 @@ func applyFilters(name string) string {
}
func getCardByUrl(path string) string {
response, _ := http.Get(path)
response, err := http.Get(path)
if err != nil {
return ""
}
defer func() {
_ = response.Body.Close()
}()
data, _ := ioutil.ReadAll(response.Body)
var v card
err := json.Unmarshal(data, &v)
err = json.Unmarshal(data, &v)
if err != nil {
return ""
}

View file

@ -1,10 +1,12 @@
package cardsinfo
import (
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestGetNameByCardId(t *testing.T) {
@ -64,3 +66,12 @@ func TestGetOriginalName_DoubleSide(t *testing.T) {
name := f.GetOriginalName("card")
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
}
func TestGetOriginalName_Error(t *testing.T) {
defer gock.Off()
gock.New(scryfallUrl + "/cards/named?fuzzy=card").ReplyError(errors.New("internal server error"))
f := &Fetcher{}
name := f.GetOriginalName("card")
assert.Equal(t, "", name)
}