forked from rust-cli/team
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves rust-cli#39
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
# Exit codes | ||
|
||
A program doesn't always success. | ||
And when an error occurs, | ||
you should make sure to use the correct ways to emit the necessary information. | ||
In addition to | ||
[telling the user about errors](human-communication.md), | ||
on most systems, | ||
when a process exits, | ||
it also emits an exit code | ||
(an integer between 0 and 255). | ||
You should try to emit the correct code | ||
for your program's state. | ||
For example, | ||
in the ideal case when your program succeeded, | ||
it should exit with `0`. | ||
|
||
When an error ocurred, it gets a bit more complicated, though. | ||
In the wild, | ||
a lot of tools exit with `1` when a general failure ocurred. | ||
Currently, Rust set and exit code of `101` when the process panicked. | ||
Beyond that, many people have done many things in their programs. | ||
|
||
So, what to do? | ||
The BSD ecosystem has collected a common definition for their exit codes | ||
in a system-provided header file called [`sysexits.h`] | ||
The Rust library [`exitcode`] provides these same codes | ||
ready to be used in your application. | ||
Please see it's API documentation for the possible values to use. | ||
|
||
One way to use it is like this: | ||
|
||
```rust | ||
fn main() { | ||
// ...actual work... | ||
match result { | ||
Ok(_) => { | ||
println!("Done!"); | ||
std::process::exit(exitcode::OK); | ||
} | ||
Err(CustomError::CantReadConfig(e)) => { | ||
eprintln!("Error: {}", e); | ||
std::process::exit(exitcode::CONFIG); | ||
} | ||
Err(e) => { | ||
eprintln!("Error: {}", e); | ||
std::process::exit(exitcode::DATAERR); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|
||
[`exitcode`]: https://crates.io/crates/exitcode | ||
[`sysexits.h`]: https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+11.2-stable&arch=default&format=html |