@@ -12,28 +12,16 @@ import 'file_system.dart';
12
12
// There is no exception corresponding to the POSIX `EISDIR` error code because
13
13
// there is no corresponding Windows error code.
14
14
15
- /// An error related to call to the operating system or an intermediary library,
16
- /// such as libc.
17
- class SystemCallError {
18
- /// The name of the system call, such as `open` or `CreateFile` .
19
- final String systemCall;
20
-
21
- /// The operating-system defined code for the error, such as 2 for
22
- /// `ERROR_FILE_NOT_FOUND` on Windows.
23
- final int errorCode;
24
-
25
- /// The operating-system description of the error, such as
26
- /// "The system cannot find the file specified."
27
- final String message;
28
-
29
- const SystemCallError (this .systemCall, this .errorCode, this .message);
30
- }
31
-
32
15
/// Exception thrown when a file operation fails.
33
16
class IOFileException implements Exception {
34
- // A description of the failed operation.
17
+ // A description of the failed operation, such as
18
+ //`'No such file or directory'`.
35
19
final String message;
36
20
21
+ /// The operating-system defined code for the error, such as 2 for
22
+ /// `ERROR_FILE_NOT_FOUND` on Windows.
23
+ final int ? errorCode;
24
+
37
25
/// A path provided in a failed file operation.
38
26
///
39
27
/// For file operations that involve a single path
@@ -60,28 +48,37 @@ class IOFileException implements Exception {
60
48
/// The underlying system call that failed.
61
49
///
62
50
/// Can be `null` if the exception is not raised due to a failed system call.
63
- final SystemCallError ? systemCall;
51
+ final String ? systemCall;
64
52
65
53
const IOFileException (
66
54
this .message, {
67
55
this .path1,
68
56
this .path2,
69
57
this .systemCall,
58
+ this .errorCode,
70
59
});
71
60
72
61
String _toStringHelper (String className) {
73
62
final sb = StringBuffer ('$className : $message ' );
63
+ // IOFileException: No such file or directory
74
64
if (path1 != null ) {
75
- sb.write (', path1="$path1 "' );
65
+ // IOFileException: No such file or directory: "a"
66
+ sb.write (': "$path1 "' );
76
67
}
77
68
if (path2 != null ) {
78
- sb.write (', path2="$path2 "' );
79
- }
80
- if (systemCall case final call? ) {
81
- sb.write (
82
- ' (${call .systemCall }: ${call .message }, errorCode=${call .errorCode })' ,
83
- );
69
+ // IOFileException: No such file or directory: "a" -> "b"
70
+ sb.write (' -> "$path2 "' );
84
71
}
72
+ sb.write (switch ((systemCall, errorCode)) {
73
+ // ... or directory: "a" -> "b"
74
+ (null , null ) => '' ,
75
+ // ... or directory: "a" -> "b" [errorCode: 2]
76
+ (null , final error? ) => ' [errorCode: $error ]' ,
77
+ // ... or directory: "a" -> "b" [renameat failed]
78
+ (final call? , null ) => ' [$call failed]' ,
79
+ // ... or directory: "a" -> "b" [renameat failed with errorCode: 2]
80
+ (final error? , final call? ) => ' [$error failed with errorCode: $call ]' ,
81
+ });
85
82
return sb.toString ();
86
83
}
87
84
@@ -101,6 +98,7 @@ class DirectoryNotEmptyException extends IOFileException {
101
98
super .path1,
102
99
super .path2,
103
100
super .systemCall,
101
+ super .errorCode,
104
102
});
105
103
106
104
@override
@@ -119,29 +117,13 @@ class DiskFullException extends IOFileException {
119
117
super .path1,
120
118
super .path2,
121
119
super .systemCall,
120
+ super .errorCode,
122
121
});
123
122
124
123
@override
125
124
String toString () => _toStringHelper ('DiskFullException' );
126
125
}
127
126
128
- /// Exception thrown when a file operation (such as
129
- /// `FileSystem.remove` ) is requested on directory.
130
- ///
131
- /// This exception corresponds to errors such as `EISDIR` on POSIX systems and
132
- /// `ERROR_DIRECTORY` on Windows.
133
- class IsADirectoryException extends IOFileException {
134
- const IsADirectoryException (
135
- super .message, {
136
- super .path1,
137
- super .path2,
138
- super .systemCall,
139
- });
140
-
141
- @override
142
- String toString () => _toStringHelper ('IsADirectoryException' );
143
- }
144
-
145
127
/// Exception thrown when a directory operation (such as
146
128
/// [FileSystem.removeDirectory] ) is requested on a non-directory.
147
129
///
@@ -153,6 +135,7 @@ class NotADirectoryException extends IOFileException {
153
135
super .path1,
154
136
super .path2,
155
137
super .systemCall,
138
+ super .errorCode,
156
139
});
157
140
158
141
@override
@@ -170,6 +153,7 @@ class PathAccessException extends IOFileException {
170
153
super .path1,
171
154
super .path2,
172
155
super .systemCall,
156
+ super .errorCode,
173
157
});
174
158
175
159
@override
@@ -187,6 +171,7 @@ class PathExistsException extends IOFileException {
187
171
super .path1,
188
172
super .path2,
189
173
super .systemCall,
174
+ super .errorCode,
190
175
});
191
176
192
177
@override
@@ -204,6 +189,7 @@ class PathNotFoundException extends IOFileException {
204
189
super .path1,
205
190
super .path2,
206
191
super .systemCall,
192
+ super .errorCode,
207
193
});
208
194
209
195
@override
@@ -220,6 +206,7 @@ class TooManyOpenFilesException extends IOFileException {
220
206
super .path1,
221
207
super .path2,
222
208
super .systemCall,
209
+ super .errorCode,
223
210
});
224
211
225
212
@override
0 commit comments