Skip to content

Commit aec4919

Browse files
author
Leandro
committed
examples: demonstrate sentinel error handling with errors.Is
Makes the ErrNoFace/ErrNotSingleFace check visible where it actually happens (AddImageToDataset), instead of only in the README's Errors section.
1 parent 4c2041a commit aec4919

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func main() {
204204
package main
205205

206206
import (
207+
"errors"
207208
"fmt"
208209
"path/filepath"
209210

@@ -217,9 +218,11 @@ func addFile(rec *recognizer.Recognizer, Path, Id string) {
217218

218219
err := rec.AddImageToDataset(Path, Id)
219220

220-
if err != nil {
221+
switch {
222+
case errors.Is(err, recognizer.ErrNoFace), errors.Is(err, recognizer.ErrNotSingleFace):
223+
fmt.Printf("%s: not exactly one face, skipping\n", Path)
224+
case err != nil:
221225
fmt.Println(err)
222-
return
223226
}
224227

225228
}

examples/recognition/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"path/filepath"
67

@@ -12,14 +13,13 @@ const dataDir = "models"
1213

1314
func addFile(rec *recognizer.Recognizer, Path, Id string) {
1415

15-
// AddImageToDataset returns recognizer.ErrNoFace/ErrNotSingleFace for
16-
// images that don't have exactly one face -- check with errors.Is if
17-
// you need to tell those apart; here we just log whatever comes back.
1816
err := rec.AddImageToDataset(Path, Id)
1917

20-
if err != nil {
18+
switch {
19+
case errors.Is(err, recognizer.ErrNoFace), errors.Is(err, recognizer.ErrNotSingleFace):
20+
fmt.Printf("%s: not exactly one face, skipping\n", Path)
21+
case err != nil:
2122
fmt.Println(err)
22-
return
2323
}
2424

2525
}

0 commit comments

Comments
 (0)