Skip to content

Commit 56cf33d

Browse files
authored
Switch to using package exceptions instead of dart:io exceptions. (#253)
1 parent 525ce08 commit 56cf33d

21 files changed

+818
-569
lines changed

pkgs/io_file/constants.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
"EACCES",
3939
"EEXIST",
4040
"EINTR",
41+
"EMFILE",
4142
"ENOENT",
43+
"ENOSPC",
44+
"ENOTDIR",
45+
"ENOTEMPTY",
4246
"EPERM"
4347
],
4448
"<fcntl.h>": [

pkgs/io_file/lib/io_file.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
export 'src/exceptions.dart';
56
export 'src/file_system.dart';
67
export 'src/vm_file_system_property.dart'
78
if (dart.library.html) 'src/web_file_system_property.dart';

pkgs/io_file/lib/src/constant_bindings.g.dart

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/io_file/lib/src/constants.g.dart

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/io_file/lib/src/exceptions.dart

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,16 @@ import 'file_system.dart';
1212
// There is no exception corresponding to the POSIX `EISDIR` error code because
1313
// there is no corresponding Windows error code.
1414

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-
3215
/// Exception thrown when a file operation fails.
3316
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'`.
3519
final String message;
3620

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+
3725
/// A path provided in a failed file operation.
3826
///
3927
/// For file operations that involve a single path
@@ -60,28 +48,37 @@ class IOFileException implements Exception {
6048
/// The underlying system call that failed.
6149
///
6250
/// Can be `null` if the exception is not raised due to a failed system call.
63-
final SystemCallError? systemCall;
51+
final String? systemCall;
6452

6553
const IOFileException(
6654
this.message, {
6755
this.path1,
6856
this.path2,
6957
this.systemCall,
58+
this.errorCode,
7059
});
7160

7261
String _toStringHelper(String className) {
7362
final sb = StringBuffer('$className: $message');
63+
// IOFileException: No such file or directory
7464
if (path1 != null) {
75-
sb.write(', path1="$path1"');
65+
// IOFileException: No such file or directory: "a"
66+
sb.write(': "$path1"');
7667
}
7768
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"');
8471
}
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+
});
8582
return sb.toString();
8683
}
8784

@@ -101,6 +98,7 @@ class DirectoryNotEmptyException extends IOFileException {
10198
super.path1,
10299
super.path2,
103100
super.systemCall,
101+
super.errorCode,
104102
});
105103

106104
@override
@@ -119,29 +117,13 @@ class DiskFullException extends IOFileException {
119117
super.path1,
120118
super.path2,
121119
super.systemCall,
120+
super.errorCode,
122121
});
123122

124123
@override
125124
String toString() => _toStringHelper('DiskFullException');
126125
}
127126

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-
145127
/// Exception thrown when a directory operation (such as
146128
/// [FileSystem.removeDirectory]) is requested on a non-directory.
147129
///
@@ -153,6 +135,7 @@ class NotADirectoryException extends IOFileException {
153135
super.path1,
154136
super.path2,
155137
super.systemCall,
138+
super.errorCode,
156139
});
157140

158141
@override
@@ -170,6 +153,7 @@ class PathAccessException extends IOFileException {
170153
super.path1,
171154
super.path2,
172155
super.systemCall,
156+
super.errorCode,
173157
});
174158

175159
@override
@@ -187,6 +171,7 @@ class PathExistsException extends IOFileException {
187171
super.path1,
188172
super.path2,
189173
super.systemCall,
174+
super.errorCode,
190175
});
191176

192177
@override
@@ -204,6 +189,7 @@ class PathNotFoundException extends IOFileException {
204189
super.path1,
205190
super.path2,
206191
super.systemCall,
192+
super.errorCode,
207193
});
208194

209195
@override
@@ -220,6 +206,7 @@ class TooManyOpenFilesException extends IOFileException {
220206
super.path1,
221207
super.path2,
222208
super.systemCall,
209+
super.errorCode,
223210
});
224211

225212
@override

0 commit comments

Comments
 (0)