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
refactor: clean up some panics in favor of returning a Result #5967
Conversation
Also make the error message whehn --genesis and --download-genesis are both given a little more clear.
#[cfg(platform = "unix")] | ||
{ | ||
use std::os::unix::fs::PermissionsExt; | ||
let perm = std::fs::Permissions::from_mode(u32::from(libc::S_IWUSR | libc::S_IRUSR)); | ||
file.set_permissions(perm).expect("Failed to set permissions for a key file."); | ||
} | ||
let str = serde_json::to_string_pretty(self).expect("Error serializing the key file."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be a good idea to print the path, where this operation failed. That, would make it easier to debug if something happens. Otherwise, you kind of have to modify the code, to figure out what failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yea so what I tried to do here was make the callers print out the path in config.rs, like:
network_signer
.write_to_file(&path)
.with_context(|| format!("Error writing key file to {}", path.display()))?
But now that I look at it again maybe it's easier to do that in this function here, yeah. Since we'd have to do that in every caller anyway. Maybe the way to go is to make this function return an anyhow::Result<()>
and call with_context
in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…oes wrong This was missed, but near#5967 did not interact very well with `enum FileDownloadError`, so this is an attempt to fix that, and present better error messages. The reason for getting rid of the #[from] in the HttpError variant is that it doesn't really play nicely with the anyhow {:#} formatter and gives us ugliness like: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002/config.json: error trying to connect: tcp connect error: Connection refused (os error 111): tcp connect error: Connection refused (os error 111): Connection refused (os error 111) Test Plan: Of course check that downloading the file still works, but also inject errors and make sure the error messages look ok. For reviewer convenience, error msgs corresponding to the variants below (except for RemoveTemporaryFileError. Didn't get to that one... but I think it's probably good) HttpError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002/config.json: error trying to connect: tcp connect error: Connection refused (os error 111) OpenError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002/config.json: Failed to open temporary file: No such file or directory (os error 2) at path "/asdf/.tmp2g9Pqe" WriteError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8000/config.json: Failed to write to temporary file at /tmp/asdfa/.tmpNdy4iL: Bad file descriptor (os error 9) RenameEror: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8000/config.json: Failed to rename temporary file /tmp/asdfa/.tmpRvBBEn to /tmp/asdfa/config.json : Permission denied (os error 13) UriError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002:3:a:b/config.json: Invalid URI: invalid authority
…6024) This was missed, but #5967 did not interact very well with `enum FileDownloadError`, so this is an attempt to fix that, and present better error messages. The reason for getting rid of the #[from] in the HttpError variant is that it doesn't really play nicely with the anyhow {:#} formatter and gives us ugliness like: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002/config.json: error trying to connect: tcp connect error: Connection refused (os error 111): tcp connect error: Connection refused (os error 111): Connection refused (os error 111) Test Plan: Of course check that downloading the file still works, but also inject errors and make sure the error messages look ok. For reviewer convenience, error msgs corresponding to the variants below (except for RemoveTemporaryFileError. Didn't get to that one... but I think it's probably good) HttpError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002/config.json: error trying to connect: tcp connect error: Connection refused (os error 111) OpenError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002/config.json: Failed to open temporary file: No such file or directory (os error 2) at path "/asdf/.tmp2g9Pqe" WriteError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8000/config.json: Failed to write to temporary file at /tmp/asdfa/.tmpNdy4iL: Bad file descriptor (os error 9) RenameEror: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8000/config.json: Failed to rename temporary file /tmp/asdfa/.tmpRvBBEn to /tmp/asdfa/config.json : Permission denied (os error 13) UriError: ERROR neard::cli: Failed to initialize configs: Failed to download the config file from http://localhost:8002:3:a:b/config.json: Invalid URI: invalid authority * use the #[source] attribute instead of directly formatting errors and store a PathBuf instead of a String for paths in error variants
near#5967 refactored things so that the init command wouldn't panic on normal/expected errors, but it inadvertenly changed things so that we always get exit code 0 even when the command fails, which is quite sad. So just return an `anyhow::Error` from `InitCmd::run()`
#5967 refactored things so that the init command wouldn't panic on normal/expected errors, but it inadvertenly changed things so that we always get exit code 0 even when the command fails, which is quite sad. So just return an `anyhow::Error` from `InitCmd::run()`
near#5967 refactored things so that the init command wouldn't panic on normal/expected errors, but it inadvertenly changed things so that we always get exit code 0 even when the command fails, which is quite sad. So just return an `anyhow::Error` from `InitCmd::run()`
near#5967 refactored things so that the init command wouldn't panic on normal/expected errors, but it inadvertenly changed things so that we always get exit code 0 even when the command fails, which is quite sad. So just return an `anyhow::Error` from `InitCmd::run()`
near#5967 refactored things so that the init command wouldn't panic on normal/expected errors, but it inadvertenly changed things so that we always get exit code 0 even when the command fails, which is quite sad. So just return an `anyhow::Error` from `InitCmd::run()`
Here we change things so that functions that might panic on bad user input return a result instead, so we can exit normally from the
init
command. This doesn't get rid of every case, but tries to chip away at it a little.Issue: #5485