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

refactor: clean up some panics in favor of returning a Result #5967

Merged
merged 7 commits into from Jan 6, 2022

Conversation

marcelo-gonzalez
Copy link
Contributor

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

#[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.");
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

@pmnoxx pmnoxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marcelo-gonzalez marcelo-gonzalez merged commit f39a30d into near:master Jan 6, 2022
@marcelo-gonzalez marcelo-gonzalez deleted the panic-cleanups branch January 6, 2022 14:18
marcelo-gonzalez added a commit to marcelo-gonzalez/nearcore that referenced this pull request Jan 7, 2022
…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
marcelo-gonzalez added a commit that referenced this pull request Jan 7, 2022
…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
marcelo-gonzalez added a commit to marcelo-gonzalez/nearcore that referenced this pull request Jan 11, 2023
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-bulldozer bot pushed a commit that referenced this pull request Jan 11, 2023
#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()`
nikurt pushed a commit to nikurt/nearcore that referenced this pull request Jan 15, 2023
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()`
marcelo-gonzalez added a commit to marcelo-gonzalez/nearcore that referenced this pull request Jan 26, 2023
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()`
marcelo-gonzalez added a commit to marcelo-gonzalez/nearcore that referenced this pull request Jan 26, 2023
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()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants