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

View file

@ -1,10 +1,12 @@
package cardsinfo package cardsinfo
import ( import (
"github.com/stretchr/testify/assert" "errors"
"gopkg.in/h2non/gock.v1"
"net/http" "net/http"
"testing" "testing"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
) )
func TestGetNameByCardId(t *testing.T) { func TestGetNameByCardId(t *testing.T) {
@ -64,3 +66,12 @@ func TestGetOriginalName_DoubleSide(t *testing.T) {
name := f.GetOriginalName("card") name := f.GetOriginalName("card")
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name) 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)
}