-
-
Notifications
You must be signed in to change notification settings - Fork 809
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
Improve error message when giving the wrong password for a local token #4339
Conversation
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.
Thank you!
It looks like the FailedToDecrypt
is no longer used. Let's remove that error variant because of this.
let unencrypted = encryption::decrypt_with_passphrase(encrypted.as_bytes(), &password)?; | ||
let unencrypted = encryption::decrypt_with_passphrase(encrypted.as_bytes(), &password) | ||
.map_err(|e| Error::FailedToDecryptApiKey { | ||
detail: e.to_string(), |
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.
Do not call to_string
on our error type please.
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.
I modified decrypt_with_passphrase
to return a more generic DecryptError
gleam/compiler-core/src/encryption.rs
Line 29 in 578f878
) -> Result<String, DecryptError> { |
Unfortunately some extra boilerplate is required as decrypt_with_passphrase
can fail in two different ways
gleam/compiler-core/src/encryption.rs
Lines 15 to 24 in 578f878
// the function `decrypt_with_passphrase` has two possible failure cases: | |
// - when decryption fails | |
// - when the data was decrypted succesfully but the result is not UTF-8 valid | |
#[derive(Error, Debug)] | |
pub enum DecryptError { | |
#[error("unable to decrypt message: {0}")] | |
Decrypt(#[from] age::DecryptError), | |
#[error("decrypted message is not UTF-8 valid: {0}")] | |
Io(#[from] std::string::FromUtf8Error), | |
} |
I would love to use thiserror's #[from]
macro to automatically convert a DecryptError
into a FailedToDecryptApiKey
but it is not possible because of Eq
and PartialEq
derive macros here and those not being implemented by DecryptError
variants...
Hence the manual map_err
and the .to_string
call.
An alternative will be to return directly a FailedToDecryptApiKey
from decrypt_with_passphrase
instead of a DecryptError
.
That approach would be more direct and simple, but also would make decrypt_with_passphrase
a bit less flexible.
I would be happy to refactor if you think simplicity is a better trade-off.
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.
This looks great!! Thank you!!
compiler-core/src/error.rs
Outdated
@@ -321,6 +321,9 @@ file_names.iter().map(|x| x.as_str()).join(", "))] | |||
|
|||
#[error("Failed to decrypt data")] | |||
FailedToDecrypt { detail: String }, | |||
|
|||
#[error("Failed to decrypt API key")] | |||
FailedToDecryptApiKey { detail: String }, |
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.
Could be any API key! FailedToDecryptLocalHexApiKey or such? The error attribute needs to say this info also.
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.
modified both, the error variant name and the error attribute
gleam/compiler-core/src/error.rs
Lines 322 to 323 in 578f878
#[error("Failed to decrypt local Hex API key")] | |
FailedToDecryptLocalHexApiKey { detail: String }, |
compiler-core/src/error.rs
Outdated
@@ -1487,6 +1490,21 @@ The error from the encryption library was: | |||
}] | |||
} | |||
|
|||
Error::FailedToDecryptApiKey { detail } => { | |||
let text = wrap_format!("Unable to decrypt the local Hex token with the given password. |
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.
We call it an API key, not a token 🙏
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.
modified token
-> API key
gleam/compiler-core/src/error.rs
Line 1476 in 578f878
let text = wrap_format!("Unable to decrypt the local Hex API key with the given password. |
compiler-core/src/error.rs
Outdated
{detail}" | ||
); | ||
vec![Diagnostic { | ||
title: "Failed to decrypt Hex token".into(), |
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.
Here too
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.
modified Hex token
-> local Hex API key
gleam/compiler-core/src/error.rs
Line 1482 in 578f878
title: "Failed to decrypt local Hex API key".into(), |
I removed that error variant and in exchange introduced In a similar fashion, I also went out on a limb and replaced the generic encryption "data" error gleam/compiler-core/src/error.rs Lines 319 to 320 in fc5daa9
with a narrower version, similar to gleam/compiler-core/src/error.rs Lines 319 to 320 in 578f878
Let me know what do you think! Thank you for your patience 🙏🏻 |
Is this ready for review? Press the button if so! :) |
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.
Wonderful! Thank you very much!
let unencrypted = encryption::decrypt_with_passphrase(encrypted.as_bytes(), &password)?; | ||
let unencrypted = encryption::decrypt_with_passphrase(encrypted.as_bytes(), &password) | ||
.map_err(|e| Error::FailedToDecryptApiKey { | ||
detail: e.to_string(), |
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.
This looks great!! Thank you!!
Head branch was pushed to by a user without write access
@lpil unfortunately I accepted your suggestion before seeing your commit, that invalidated auto-merge. I tried to remove the commit by force pushing, but that didn't help either. Anyway, hopefully everything is in order now. |
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.
Thanks again!
Adds a new error variant
Error::FailedToDecryptApiKey
specifically to handle the case when the decryption of the Hex API token fails.Hopefully, the changes will make the error message a bit more friendly and possibly more useful.
resolves #4317