Skip to content

feat(hugr-cli): Nicer error when passing a non-envelope file #2007

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

Merged
merged 1 commit into from
Mar 20, 2025
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
9 changes: 6 additions & 3 deletions hugr-cli/src/hugr_io.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Input/output arguments for the HUGR CLI.

use clio::Input;
use hugr::envelope::read_envelope;
use hugr::envelope::{read_envelope, EnvelopeError};
use hugr::extension::ExtensionRegistry;
use hugr::package::Package;
use hugr::{Extension, Hugr};
Expand Down Expand Up @@ -48,8 +48,11 @@ impl HugrInputArgs {
pub fn get_package(&mut self) -> Result<Package, CliError> {
let extensions = self.load_extensions()?;
let buffer = BufReader::new(&mut self.input);
let (_, pkg) = read_envelope(buffer, &extensions)?;
Ok(pkg)
match read_envelope(buffer, &extensions) {
Ok((_, pkg)) => Ok(pkg),
Err(EnvelopeError::MagicNumber { .. }) => Err(CliError::NotAnEnvelope),
Err(e) => Err(CliError::Envelope(e)),
}
}

/// Read a hugr JSON file from the input.
Expand Down
5 changes: 5 additions & 0 deletions hugr-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ pub enum CliError {
#[display("Error decoding HUGR envelope: {_0}")]
/// Errors produced by the `validate` subcommand.
Envelope(EnvelopeError),
/// Pretty error when the user passes a non-envelope file.
#[display(
"Input file is not a HUGR envelope. Invalid magic number.\n\nUse `--hugr-json` to read a raw HUGR JSON file instead."
)]
NotAnEnvelope,
}

/// Other arguments affecting the HUGR CLI runtime.
Expand Down
Loading