Skip to content

Switch to using package exceptions instead of dart:io exceptions. #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/io_file/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
"EACCES",
"EEXIST",
"EINTR",
"EMFILE",
"ENOENT",
"ENOSPC",
"ENOTDIR",
"ENOTEMPTY",
"EPERM"
],
"<fcntl.h>": [
Expand Down
1 change: 1 addition & 0 deletions pkgs/io_file/lib/io_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

export 'src/exceptions.dart';
export 'src/file_system.dart';
export 'src/vm_file_system_property.dart'
if (dart.library.html) 'src/web_file_system_property.dart';
12 changes: 12 additions & 0 deletions pkgs/io_file/lib/src/constant_bindings.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions pkgs/io_file/lib/src/constants.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 30 additions & 43 deletions pkgs/io_file/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,16 @@ import 'file_system.dart';
// There is no exception corresponding to the POSIX `EISDIR` error code because
// there is no corresponding Windows error code.

/// An error related to call to the operating system or an intermediary library,
/// such as libc.
class SystemCallError {
/// The name of the system call, such as `open` or `CreateFile`.
final String systemCall;

/// The operating-system defined code for the error, such as 2 for
/// `ERROR_FILE_NOT_FOUND` on Windows.
final int errorCode;

/// The operating-system description of the error, such as
/// "The system cannot find the file specified."
final String message;

const SystemCallError(this.systemCall, this.errorCode, this.message);
}

/// Exception thrown when a file operation fails.
class IOFileException implements Exception {
// A description of the failed operation.
// A description of the failed operation, such as
//`'No such file or directory'`.
final String message;

/// The operating-system defined code for the error, such as 2 for
/// `ERROR_FILE_NOT_FOUND` on Windows.
final int? errorCode;

/// A path provided in a failed file operation.
///
/// For file operations that involve a single path
Expand All @@ -60,28 +48,37 @@ class IOFileException implements Exception {
/// The underlying system call that failed.
///
/// Can be `null` if the exception is not raised due to a failed system call.
final SystemCallError? systemCall;
final String? systemCall;

const IOFileException(
this.message, {
this.path1,
this.path2,
this.systemCall,
this.errorCode,
});

String _toStringHelper(String className) {
final sb = StringBuffer('$className: $message');
// IOFileException: No such file or directory
if (path1 != null) {
sb.write(', path1="$path1"');
// IOFileException: No such file or directory: "a"
sb.write(': "$path1"');
}
if (path2 != null) {
sb.write(', path2="$path2"');
}
if (systemCall case final call?) {
sb.write(
' (${call.systemCall}: ${call.message}, errorCode=${call.errorCode})',
);
// IOFileException: No such file or directory: "a" -> "b"
sb.write(' -> "$path2"');
}
sb.write(switch ((systemCall, errorCode)) {
// ... or directory: "a" -> "b"
(null, null) => '',
// ... or directory: "a" -> "b" [errorCode: 2]
(null, final error?) => ' [errorCode: $error]',
// ... or directory: "a" -> "b" [renameat failed]
(final call?, null) => ' [$call failed]',
// ... or directory: "a" -> "b" [renameat failed with errorCode: 2]
(final error?, final call?) => ' [$error failed with errorCode: $call]',
});
return sb.toString();
}

Expand All @@ -101,6 +98,7 @@ class DirectoryNotEmptyException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
Expand All @@ -119,29 +117,13 @@ class DiskFullException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
String toString() => _toStringHelper('DiskFullException');
}

/// Exception thrown when a file operation (such as
/// `FileSystem.remove`) is requested on directory.
///
/// This exception corresponds to errors such as `EISDIR` on POSIX systems and
/// `ERROR_DIRECTORY` on Windows.
class IsADirectoryException extends IOFileException {
const IsADirectoryException(
super.message, {
super.path1,
super.path2,
super.systemCall,
});

@override
String toString() => _toStringHelper('IsADirectoryException');
}

/// Exception thrown when a directory operation (such as
/// [FileSystem.removeDirectory]) is requested on a non-directory.
///
Expand All @@ -153,6 +135,7 @@ class NotADirectoryException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
Expand All @@ -170,6 +153,7 @@ class PathAccessException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
Expand All @@ -187,6 +171,7 @@ class PathExistsException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
Expand All @@ -204,6 +189,7 @@ class PathNotFoundException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
Expand All @@ -220,6 +206,7 @@ class TooManyOpenFilesException extends IOFileException {
super.path1,
super.path2,
super.systemCall,
super.errorCode,
});

@override
Expand Down
Loading
Loading