...
Source file src/os/error.go
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 )
27
28 func errInvalid() error { return oserror.ErrInvalid }
29 func errPermission() error { return oserror.ErrPermission }
30 func errExist() error { return oserror.ErrExist }
31 func errNotExist() error { return oserror.ErrNotExist }
32 func errClosed() error { return oserror.ErrClosed }
33 func errNoDeadline() error { return poll.ErrNoDeadline }
34
35 type timeout interface {
36 Timeout() bool
37 }
38
39
40 type PathError struct {
41 Op string
42 Path string
43 Err error
44 }
45
46 func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
47
48 func (e *PathError) Unwrap() error { return e.Err }
49
50
51 func (e *PathError) Timeout() bool {
52 t, ok := e.Err.(timeout)
53 return ok && t.Timeout()
54 }
55
56
57 type SyscallError struct {
58 Syscall string
59 Err error
60 }
61
62 func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() }
63
64 func (e *SyscallError) Unwrap() error { return e.Err }
65
66
67 func (e *SyscallError) Timeout() bool {
68 t, ok := e.Err.(timeout)
69 return ok && t.Timeout()
70 }
71
72
73
74
75 func NewSyscallError(syscall string, err error) error {
76 if err == nil {
77 return nil
78 }
79 return &SyscallError{syscall, err}
80 }
81
82
83
84
85 func IsExist(err error) bool {
86 return underlyingErrorIs(err, ErrExist)
87 }
88
89
90
91
92 func IsNotExist(err error) bool {
93 return underlyingErrorIs(err, ErrNotExist)
94 }
95
96
97
98
99 func IsPermission(err error) bool {
100 return underlyingErrorIs(err, ErrPermission)
101 }
102
103
104
105 func IsTimeout(err error) bool {
106 terr, ok := underlyingError(err).(timeout)
107 return ok && terr.Timeout()
108 }
109
110 func underlyingErrorIs(err, target error) bool {
111
112
113
114 err = underlyingError(err)
115 if err == target {
116 return true
117 }
118
119 e, ok := err.(syscallErrorType)
120 return ok && e.Is(target)
121 }
122
123
124 func underlyingError(err error) error {
125 switch err := err.(type) {
126 case *PathError:
127 return err.Err
128 case *LinkError:
129 return err.Err
130 case *SyscallError:
131 return err.Err
132 }
133 return err
134 }
135
View as plain text