Skip to content

Commit

Permalink
Fixing Oxford fallback attempting empty search
Browse files Browse the repository at this point in the history
Also clarifying fallback cases in code comments.
  • Loading branch information
Rican7 committed Mar 6, 2024
1 parent 8f4c21c commit 611f721
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions source/oxford/oxford.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (a *api) Define(word string) (source.DictionaryResults, error) {

if err = validateResponse(word, httpResponse); err != nil {
if _, isEmptyResult := err.(*source.EmptyResultError); isEmptyResult {
// Empty (404) result
// Try and automatically fallback
return a.apiSearchFallback(word)
}

Expand All @@ -108,6 +110,8 @@ func (a *api) Define(word string) (source.DictionaryResults, error) {
}

if len(response.Results) < 1 {
// Valid (200), but empty result
// Try and automatically fallback
return a.apiSearchFallback(word)
}

Expand Down Expand Up @@ -199,8 +203,8 @@ func (a *api) apiSearchFallback(word string) (source.DictionaryResults, error) {
}
}

if strings.EqualFold(word, fallbackWord) {
// Prevent matching against the same word
if fallbackWord == "" || strings.EqualFold(word, fallbackWord) {
// Prevent searching for empty or the same word
return nil, &source.EmptyResultError{Word: word}
}

Expand Down

0 comments on commit 611f721

Please sign in to comment.