From 0db7e7c5331a202183423b52db2ce3955c522edd Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Mon, 5 Aug 2019 17:55:11 -0700 Subject: [PATCH] Catch intermittent failure with HIBP module --- modules/hibp/client.go | 14 +++++++------- modules/hibp/hibp_status.go | 4 ++++ utils/text_test.go | 4 ---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/hibp/client.go b/modules/hibp/client.go index d1505dcb..e790fb7e 100644 --- a/modules/hibp/client.go +++ b/modules/hibp/client.go @@ -75,23 +75,23 @@ func (widget *Widget) fetchForAccount(account string, since string) (*Status, er } func (widget *Widget) parseResponseBody(account string, body []byte) (*Status, error) { - // If the body is empty then there's no breaches + breaches := []Breach{} + stat := NewStatus(account, breaches) + if len(body) == 0 { - stat := NewStatus(account, []Breach{}) + // If the body is empty then there's no breaches return stat, nil } - // Else we have breaches for this account - breaches := make([]Breach, 0) - jsonErr := json.Unmarshal(body, &breaches) if jsonErr != nil { - return nil, jsonErr + return stat, jsonErr } breaches = widget.filterBreaches(breaches) + stat.Breaches = breaches - return NewStatus(account, breaches), nil + return stat, nil } func (widget *Widget) filterBreaches(breaches []Breach) []Breach { diff --git a/modules/hibp/hibp_status.go b/modules/hibp/hibp_status.go index 6220b503..84ef9081 100644 --- a/modules/hibp/hibp_status.go +++ b/modules/hibp/hibp_status.go @@ -24,5 +24,9 @@ func (stat *Status) HasBeenCompromised() bool { // Len returns the number of breaches found for the specified account func (stat *Status) Len() int { + if stat == nil || stat.Breaches == nil { + return 0 + } + return len(stat.Breaches) } diff --git a/utils/text_test.go b/utils/text_test.go index 5bd3fb2f..86e4f77b 100644 --- a/utils/text_test.go +++ b/utils/text_test.go @@ -12,10 +12,6 @@ func Test_CenterText(t *testing.T) { Equal(t, " cat ", CenterText("cat", 9)) } -func Test_HighlightableHelper(t *testing.T) { - -} - func Test_RowPadding(t *testing.T) { Equal(t, "", RowPadding(0, 0)) Equal(t, "", RowPadding(5, 2))