Skip to content

Commit c1bbb73

Browse files
committed
improve error handling
1 parent 41473b7 commit c1bbb73

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

oui/db.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package oui
22

33
import (
44
"database/sql"
5+
"errors"
56
"fmt"
7+
"log"
68
"strconv"
79
"strings"
810

@@ -177,38 +179,48 @@ func (ouidb *OUIDB) Count() (count int64, err error) {
177179
return
178180
}
179181

180-
func (ouidb *OUIDB) Find(search string) (matches []*VendorDef, err error) {
182+
func (ouidb *OUIDB) Find(search string) ([]*VendorDef, error) {
181183
mac, err := macaddr.ParseMACAddress(search)
182184
if err != nil {
183-
return matches, err
185+
return nil, err
184186
}
185187
q := fmt.Sprintf("SELECT prefix,length,org,registry FROM %s WHERE prefix LIKE '%s%%'", ouidb.Version, mac.OUI())
188+
log.Println(q)
186189
rows, err := ouidb.Connection.Query(q)
187190
if err != nil {
188-
return matches, err
191+
return nil, err
189192
}
190193

191194
defer rows.Close()
192195

196+
errs := make([]error, 0)
197+
matches := make([]*VendorDef, 0)
198+
193199
for rows.Next() {
194200
var prefix string
195201
var length int
196202
var org string
197203
var reg string
198204
err = rows.Scan(&prefix, &length, &org, &reg)
199205
if err != nil {
200-
return matches, err
206+
errs = append(errs, err)
207+
continue
201208
}
202209
def := &VendorDef{Prefix: prefix, Length: length, Org: org, Registry: reg}
203210
_, mp, err := macaddr.ParseMACPrefix(def.PrefixString())
204211
if err != nil {
205-
return matches, err
212+
errs = append(errs, err)
213+
continue
206214
}
207215
_, failure := mp.Match(search)
208216
if failure == nil {
209217
matches = append(matches, def)
210218
}
211219
}
220+
err = errors.Join(errs...)
221+
if err != nil {
222+
return nil, err
223+
}
212224
return matches, nil
213225
}
214226

0 commit comments

Comments
 (0)