-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
45 lines (40 loc) · 1.05 KB
/
context.go
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
38
39
40
41
42
43
44
45
package errs
import (
"context"
"errors"
)
// IsContextCanceled checks if the context Done channel is closed
// and if the context error unwraps to context.Canceled.
func IsContextCanceled(ctx context.Context) bool {
select {
case <-ctx.Done():
return errors.Is(ctx.Err(), context.Canceled)
default:
return false
}
}
// IsContextDeadlineExceeded checks if the context Done channel is closed
// and if the context error unwraps to context.DeadlineExceeded.
func IsContextDeadlineExceeded(ctx context.Context) bool {
select {
case <-ctx.Done():
return errors.Is(ctx.Err(), context.DeadlineExceeded)
default:
return false
}
}
// IsContextDone checks if the context Done channel is closed.
func IsContextDone(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
// IsContextError returns true if err unwraps to
// context.Canceled or context.DeadlineExceeded.
func IsContextError(err error) bool {
return err != nil &&
(errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded))
}