Skip to content

Commit

Permalink
fix: ovh error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Sep 8, 2022
1 parent c9a63ac commit 878c771
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
32 changes: 23 additions & 9 deletions lib/remote/suppliers/ovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package suppliers

import (
"encoding/json"
"errors"
"fmt"
"github.com/sundowndev/phoneinfoga/v2/lib/number"
"net/http"
Expand All @@ -15,15 +16,19 @@ type OVHSupplierInterface interface {

// OVHAPIResponseNumber is a type that describes an OVH number range
type OVHAPIResponseNumber struct {
MatchingCriteria interface{}
City string
ZneList []string
InternationalNumber string
Country string
AskedCity interface{}
ZipCode string
Number string
Prefix int
MatchingCriteria interface{} `json:"matchingCriteria"`
City string `json:"city"`
ZneList []string `json:"zne-list"`
InternationalNumber string `json:"internationalNumber"`
Country string `json:"country"`
AskedCity interface{} `json:"askedCity"`
ZipCode string `json:"zipCode"`
Number string `json:"number"`
Prefix int `json:"prefix"`
}

type OVHAPIErrorResponse struct {
Message string `json:"message"`
}

// OVHScannerResponse is the OVH scanner response
Expand Down Expand Up @@ -54,6 +59,15 @@ func (s *OVHSupplier) Search(num number.Number) (*OVHScannerResponse, error) {
}
defer response.Body.Close()

if response.StatusCode >= 400 {
var result OVHAPIErrorResponse
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return nil, err
}
return nil, errors.New(result.Message)
}

// Fill the response with the data from the JSON
var results []OVHAPIResponseNumber

Expand Down
13 changes: 11 additions & 2 deletions lib/remote/suppliers/ovh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ func TestOVHSupplierError(t *testing.T) {
func TestOVHSupplierCountryCodeError(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution

num, _ := number.NewNumber("15556661212")
gock.New("https://api.ovh.com").
Get("/1.0/telephony/number/detailedZones").
MatchParam("country", "co").
Reply(400).
JSON(OVHAPIErrorResponse{Message: "[country] Given data (co) does not belong to the NumberCountryEnum enumeration"})

num, err := number.NewNumber("+575556661212")
if err != nil {
t.Fatal(err)
}

s := NewOVHSupplier()

got, err := s.Search(*num)
assert.Nil(t, got)
assert.EqualError(t, err, "country code +1 wasn't recognized")
assert.EqualError(t, err, "[country] Given data (co) does not belong to the NumberCountryEnum enumeration")
}

0 comments on commit 878c771

Please sign in to comment.