example.go 868 Bytes
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
package main

import (
	"fmt"

Jeromy's avatar
Jeromy committed
6
	tec "gx/QmWtLNgjHvFnRHcHUheAMGx4sLYYYGSacNA3eG52ywy2UQ/go-temp-err-catcher"
Jeromy's avatar
Jeromy committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
)

var (
	ErrTemp  = tec.ErrTemporary{fmt.Errorf("ErrTemp")}
	ErrSkip  = fmt.Errorf("ErrSkip")
	ErrOther = fmt.Errorf("ErrOther")
)

func main() {
	var normal tec.TempErrCatcher
	var skipper tec.TempErrCatcher
	skipper.IsTemp = func(e error) bool {
		return e == ErrSkip
	}

	fmt.Println("trying normal (uses Temporary interface)")
	tryTec(normal)
	fmt.Println("")
	fmt.Println("trying skipper (uses our IsTemp function)")
	tryTec(skipper)
}

func tryTec(c tec.TempErrCatcher) {
	errs := []error{
		ErrTemp,
		ErrSkip,
		ErrOther,
		ErrTemp,
		ErrSkip,
		ErrOther,
	}

	for _, e := range errs {
		if c.IsTemporary(e) {
			fmt.Printf("\tIsTemporary: true  - skipped     %s\n", e)
			continue
		}

		fmt.Printf("\tIsTemporary: false - not skipped %s\n", e)
	}
}