Skip to content

feat: Use new enhanced enums for ExitCode #2078

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 2 commits 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
1 change: 1 addition & 0 deletions pkgs/io/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.1.0-wip

* Add a `deepCopyLinks` argument to `copyPath` and `copyPathSync`.
* Make ExitCode a real enum instead of a class with statics.

## 1.0.5

Expand Down
39 changes: 18 additions & 21 deletions pkgs/io/lib/src/exit_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,75 @@
/// Exit code constants.
///
/// [Source](https://www.freebsd.org/cgi/man.cgi?query=sysexits).
class ExitCode {
enum ExitCode {
/// Command completed successfully.
static const success = ExitCode._(0, 'success');
success(0),

/// Command was used incorrectly.
///
/// This may occur if the wrong number of arguments was used, a bad flag, or
/// bad syntax in a parameter.
static const usage = ExitCode._(64, 'usage');
usage(64),

/// Input data was used incorrectly.
///
/// This should occur only for user data (not system files).
static const data = ExitCode._(65, 'data');
data(65),

/// An input file (not a system file) did not exist or was not readable.
static const noInput = ExitCode._(66, 'noInput');
noInput(66),

/// User specified did not exist.
static const noUser = ExitCode._(67, 'noUser');
noUser(67),

/// Host specified did not exist.
static const noHost = ExitCode._(68, 'noHost');
noHost(68),

/// A service is unavailable.
///
/// This may occur if a support program or file does not exist. This may also
/// be used as a catch-all error when something you wanted to do does not
/// work, but you do not know why.
static const unavailable = ExitCode._(69, 'unavailable');
unavailable(69),

/// An internal software error has been detected.
///
/// This should be limited to non-operating system related errors as possible.
static const software = ExitCode._(70, 'software');
software(70),

/// An operating system error has been detected.
///
/// This intended to be used for such thing as `cannot fork` or `cannot pipe`.
static const osError = ExitCode._(71, 'osError');
osError(71),

/// Some system file (e.g. `/etc/passwd`) does not exist or could not be read.
static const osFile = ExitCode._(72, 'osFile');
osFile(72),

/// A (user specified) output file cannot be created.
static const cantCreate = ExitCode._(73, 'cantCreate');
cantCreate(73),

/// An error occurred doing I/O on some file.
static const ioError = ExitCode._(74, 'ioError');
ioError(74),

/// Temporary failure, indicating something is not really an error.
///
/// In some cases, this can be re-attempted and will succeed later.
static const tempFail = ExitCode._(75, 'tempFail');
tempFail(75),

/// You did not have sufficient permissions to perform the operation.
///
/// This is not intended for file system problems, which should use [noInput]
/// or [cantCreate], but rather for higher-level permissions.
static const noPerm = ExitCode._(77, 'noPerm');
noPerm(77),

/// Something was found in an unconfigured or misconfigured state.
static const config = ExitCode._(78, 'config');
config(78);

/// Exit code value.
final int code;

/// Name of the exit code.
final String _name;

const ExitCode._(this.code, this._name);
const ExitCode(this.code);

@override
String toString() => '$_name: $code';
String toString() => '$name: $code';
}
17 changes: 17 additions & 0 deletions pkgs/io/test/exit_code_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:io/io.dart';
import 'package:test/test.dart';

void main() {
test('ExitCode.toString', () {
expect('${ExitCode.success}', 'success: 0');
});

// Previously ExitCode was not an enum, but now it is.
test('ExitCode is an enum', () {
expect(ExitCode.success, isA<Enum>());
});
}