forked from cockroachdb/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oserror.go
120 lines (112 loc) · 4.28 KB
/
oserror.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2020 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package oserror
import (
"os"
"github.com/lcouds/errors"
"github.com/lcouds/errors/errbase"
)
// Portable analogs of some common system call errors.
//
// Errors returned from this package may be tested against these errors
// with errors.Is.
var (
ErrInvalid = os.ErrInvalid
ErrPermission = os.ErrPermission
ErrExist = os.ErrExist
ErrNotExist = os.ErrNotExist
ErrClosed = os.ErrClosed
)
// IsPermission returns a boolean indicating whether the error is
// known to report that permission is denied. It is satisfied by
// ErrPermission as well as some syscall errors.
//
// This function differs from os.IsPermission() in that it
// can identify an error through wrapping layers.
func IsPermission(err error) bool {
// errors.Is() is not able to peek through os.SyscallError,
// whereas os.IsPermission() can. Conversely, os.IsPermission()
// cannot peek through Unwrap, whereas errors.Is() can. So we
// need to try both.
if errors.Is(err, ErrPermission) || os.IsPermission(errors.UnwrapAll(err)) {
return true
}
// If a syscall errno representing ErrPermission was encoded on a
// different platform, and decoded here, then it will show up as
// neither a syscall errno here nor an ErrPermission; instead it
// shows up as an OpaqueErrno. We test this here.
if o := (*errbase.OpaqueErrno)(nil); errors.As(err, &o) {
return o.Is(ErrPermission)
}
return false
}
// IsExist returns a boolean indicating whether the error is known to report
// that a file or directory already exists. It is satisfied by ErrExist as
// well as some syscall errors.
//
// This function differs from os.IsExist() in that it
// can identify an error through wrapping layers.
func IsExist(err error) bool {
// errors.Is() is not able to peek through os.SyscallError,
// whereas os.IsExist() can. Conversely, os.IsExist()
// cannot peek through Unwrap, whereas errors.Is() can. So we
// need to try both.
if errors.Is(err, ErrExist) || os.IsExist(errors.UnwrapAll(err)) {
return true
}
// If a syscall errno representing ErrExist was encoded on a
// different platform, and decoded here, then it will show up as
// neither a syscall errno here nor an ErrExist; instead it
// shows up as an OpaqueErrno. We test this here.
if o := (*errbase.OpaqueErrno)(nil); errors.As(err, &o) {
return o.Is(ErrExist)
}
return false
}
// IsNotExist returns a boolean indicating whether the error is known to
// report that a file or directory does not exist. It is satisfied by
// ErrNotExist as well as some syscall errors.
//
// This function differs from os.IsNotExist() in that it
// can identify an error through wrapping layers.
func IsNotExist(err error) bool {
// errors.Is() is not able to peek through os.SyscallError,
// whereas os.IsNotExist() can. Conversely, os.IsNotExist()
// cannot peek through Unwrap, whereas errors.Is() can. So we
// need to try both.
if errors.Is(err, ErrNotExist) || os.IsNotExist(errors.UnwrapAll(err)) {
return true
}
// If a syscall errno representing ErrNotExist was encoded on a
// different platform, and decoded here, then it will show up as
// neither a syscall errno here nor an ErrNotExist; instead it
// shows up as an OpaqueErrno. We test this here.
if o := (*errbase.OpaqueErrno)(nil); errors.As(err, &o) {
return o.Is(ErrNotExist)
}
return false
}
// IsTimeout returns a boolean indicating whether the error is known
// to report that a timeout occurred.
//
// This function differs from os.IsTimeout() in that it
// can identify an error through wrapping layers.
func IsTimeout(err error) bool {
// os.IsTimeout() cannot peek through Unwrap. We need errors.If()
// for that.
_, ok := errors.If(err, func(err error) (interface{}, bool) {
return nil, os.IsTimeout(err)
})
return ok
}