~egtann/sjs

ref: 244f11278d79972028b907d640941ea0f687893c sjs/error.go -rw-r--r-- 671 bytes
244f1127 — Evan Tann add isc license 7 months ago
                                                                                
1
2
3
4
5
6
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
package sjs

import (
	"sync"

	"github.com/pkg/errors"
)

// OptErr is a channel that we can modify in a threadsafe way. The mutex is
// required to read or modify the underlying channel, which can be created at
// any time by the caller of the library. The exported "C" fieldname
// representating the channel is inspired by the standard library's time.Ticker
// design.
type OptErr struct {
	C chan error
	sync.RWMutex
}

func (o *OptErr) Send(err error) {
	o.RLock()
	defer o.RUnlock()

	if o.C == nil {
		return
	}
	o.C <- err
}

type Missing error

func IsMissing(err error) bool {
	switch errors.Cause(err).(type) {
	case Missing:
		return true
	}
	return false
}