fix: handle shop not found properly, log more

This commit is contained in:
Felipe Martin Garcia 2022-08-06 09:07:24 +02:00
parent 9220b50e22
commit 0e560e07ac
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
2 changed files with 10 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
@ -30,6 +31,12 @@ func main() {
product, err := m.Retrieve(r.PostForm.Get("url"))
if err != nil {
if errors.Is(err, manager.ErrShopNotFound) {
rw.WriteHeader(400)
return
}
log.Printf("error for url %s: %s", r.PostForm.Get("url"), err)
rw.WriteHeader(500)
return
}

View File

@ -7,6 +7,8 @@ import (
"github.com/fmartingr/bazaar/pkg/models"
)
var ErrShopNotFound = fmt.Errorf("shop not found for domain")
type Manager struct {
domains map[string]models.Shop
}
@ -41,7 +43,7 @@ func (m *Manager) Retrieve(productURL string) (*models.Product, error) {
shop := m.GetShop(itemUrl.Host)
if shop == nil {
return nil, fmt.Errorf("shop not found for domain")
return nil, ErrShopNotFound
}
return shop.Get(productURL)