Skip to content
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

Convert error types into productive exit codes #350

Merged
merged 4 commits into from
Mar 16, 2023
Merged
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ Language selection is based on precedence, in the following order:
* Detected from the input file's extension
* A specified query file

#### Exit Codes

The Topiary process will exit with a zero exit code upon successful
formatting. Otherwise, the following exit codes are defined:

| Reason | Code |
| :--------------------------- | ---- |
| Unspecified error | 1 |
| CLI argument parsing error | 2 |
| I/O error | 3 |
| Topiary query error | 4 |
| Source parsing error | 5 |
| Language detection error | 6 |
| Idempotency error | 7 |
| Unspecified formatting error | 8 |

#### Example

Once built, the program can be run like this:
Expand Down
39 changes: 26 additions & 13 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"tweag-topiary.cachix.org-1:8TKqya43LAfj4qNHnljLpuBnxAY/YwEBfzo3kzXxNY0="
];
};

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

Expand All @@ -34,7 +34,8 @@
let
pkgs = nixpkgs.legacyPackages.${system};
code = pkgs.callPackage ./. { inherit nixpkgs system advisory-db crane rust-overlay nix-filter; };
in {
in
{
packages = with code; {
inherit web-playground;
default = app;
Expand All @@ -48,17 +49,29 @@
enable = true;
name = "topiary";
description = "A general code formatter based on tree-sitter.";
entry = let
topiary-inplace = pkgs.writeShellApplication {
name = "topiary-inplace";
text = ''
for file; do
${code.app}/bin/topiary --in-place --input-file "$file"
done
'';
};
in "${topiary-inplace}/bin/topiary-inplace";
files = "(\\.json$)|(\\.toml$)|(\\.mli?$)";
entry =
let
topiary-inplace = pkgs.writeShellApplication {
name = "topiary-inplace";
text = ''
for FILE; do
if ${code.app}/bin/topiary --in-place --input-file "$FILE"; then
continue
else
EXIT=$?
if (( EXIT == 6 )); then
Xophmeister marked this conversation as resolved.
Show resolved Hide resolved
# Skip over language detection errors
continue
else
exit $EXIT
fi
fi
done
'';
};
in
"${topiary-inplace}/bin/topiary-inplace";
types = [ "text" ];
};
}
);
Expand Down
35 changes: 34 additions & 1 deletion src/bin/topiary/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error, fmt, io, result};
use std::{error, fmt, io, process::ExitCode, result};
use topiary::FormatterError;

/// A convenience wrapper around `std::result::Result<T, TopiaryError>`.
Expand Down Expand Up @@ -40,6 +40,39 @@ impl error::Error for TopiaryError {
}
}

impl From<TopiaryError> for ExitCode {
fn from(e: TopiaryError) -> Self {
let exit_code = match e {
// Formatting errors: Exit 8
TopiaryError::Lib(FormatterError::Formatting(_)) => 8,

// Idempotency errors: Exit 7
TopiaryError::Lib(FormatterError::Idempotence) => 7,

// Language detection errors: Exit 6
TopiaryError::Lib(FormatterError::LanguageDetection(_, _)) => 6,

// Parsing errors: Exit 5
TopiaryError::Lib(FormatterError::Parsing { .. }) => 5,

// Query errors: Exit 4
TopiaryError::Lib(FormatterError::Query(_, _)) => 4,

// I/O errors: Exit 3
TopiaryError::Lib(FormatterError::Io(_)) => 3,
TopiaryError::Bin(_, Some(CLIError::IOError(_))) => 3,

// Bad arguments: Exit 2
// (Handled by clap: https://github.com/clap-rs/clap/issues/3426)

// Anything else: Exit 1
_ => 1,
};

ExitCode::from(exit_code)
}
}

impl From<FormatterError> for TopiaryError {
fn from(e: FormatterError) -> Self {
Self::Lib(e)
Expand Down
7 changes: 5 additions & 2 deletions src/bin/topiary/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
fs::File,
io::{stdin, BufReader, BufWriter, Read},
path::PathBuf,
process::ExitCode,
};

use clap::{ArgGroup, Parser};
Expand Down Expand Up @@ -68,11 +69,13 @@ struct Args {
}

#[tokio::main]
async fn main() {
async fn main() -> ExitCode {
if let Err(e) = run().await {
print_error(&e);
std::process::exit(1);
return e.into();
}

ExitCode::SUCCESS
}

async fn run() -> CLIResult<()> {
Expand Down