Source file
src/os/error.go
Documentation: os
1
2
3
4
5 package os
6
7 import (
8 "internal/oserror"
9 "internal/poll"
10 )
11
12
13
14
15
16 var (
17
18
19 ErrInvalid = errInvalid()
20
21 ErrPermission = errPermission()
22 ErrExist = errExist()
23 ErrNotExist = errNotExist()
24 ErrClosed = errClosed()
25 ErrNoDeadline = errNoDeadline()
26 ErrDeadlineExceeded = errDeadlineExceeded()
27 )
28
29 func errInvalid() error { return oserror.ErrInvalid }
30 func errPermission() error { return oserror.ErrPermission }
31 func errExist() error { return oserror.ErrExist }
32 func errNotExist() error { return oserror.ErrNotExist }
33 func errClosed() error { return oserror.ErrClosed }
34 func errNoDeadline() error { return poll.ErrNoDeadline }
35
36
37
38
39
40
41
42
43 func errDeadlineExceeded() error { return poll.ErrDeadlineExceeded }
44
45 type timeout interface {
46 Timeout() bool
47 }
48
49
50 type PathError struct {
51 Op string
52 Path string
53 Err error
54 }
55
56 func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
57
58 func (e *PathError) Unwrap() error { return e.Err }
59
60
61 func (e *PathError) Timeout() bool {
62 t, ok := e.Err.(timeout)
63 return ok && t.Timeout()
64 }
65
66
67 type SyscallError struct {
68 Syscall string
69 Err error
70 }
71
72 func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() }
73
74 func (e *SyscallError) Unwrap() error { return e.Err }
75
76
77 func (e *SyscallError) Timeout() bool {
78 t, ok := e.Err.(timeout)
79 return ok && t.Timeout()
80 }
81
82
83
84
85 func NewSyscallError(syscall string, err error) error {
86 if err == nil {
87 return nil
88 }
89 return &SyscallError{syscall, err}
90 }
91
92
93
94
95 func IsExist(err error) bool {
96 return underlyingErrorIs(err, ErrExist)
97 }
98
99
100
101
102 func IsNotExist(err error) bool {
103 return underlyingErrorIs(err, ErrNotExist)
104 }
105
106
107
108
109 func IsPermission(err error) bool {
110 return underlyingErrorIs(err, ErrPermission)
111 }
112
113
114
115 func IsTimeout(err error) bool {
116 terr, ok := underlyingError(err).(timeout)
117 return ok && terr.Timeout()
118 }
119
120 func underlyingErrorIs(err, target error) bool {
121
122
123
124 err = underlyingError(err)
125 if err == target {
126 return true
127 }
128
129 e, ok := err.(syscallErrorType)
130 return ok && e.Is(target)
131 }
132
133
134 func underlyingError(err error) error {
135 switch err := err.(type) {
136 case *PathError:
137 return err.Err
138 case *LinkError:
139 return err.Err
140 case *SyscallError:
141 return err.Err
142 }
143 return err
144 }
145
View as plain text