Skip to content

Commit

Permalink
fix: use display for printing external errors
Browse files Browse the repository at this point in the history
fixes #365
  • Loading branch information
mike-engel committed Nov 25, 2024
1 parent de949a8 commit 3becd97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- Add completions for nushell
- Use Display trait to print external errors

# 6.2.0

Expand Down
39 changes: 14 additions & 25 deletions src/translators/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,46 +188,35 @@ pub fn print_decoded_token(
match err {
JWTError::External(ext_err) => {
match ext_err.kind() {
ErrorKind::InvalidToken => {
bunt::println!("{$red+bold}The JWT provided is invalid{/$}")
}
ErrorKind::InvalidSignature => {
bunt::eprintln!("{$red+bold}The JWT provided has an invalid signature{/$}")
}
ErrorKind::InvalidRsaKey(_) => {
bunt::eprintln!("{$red+bold}The secret provided isn't a valid RSA key{/$}")
}
ErrorKind::InvalidEcdsaKey => {
bunt::eprintln!("{$red+bold}The secret provided isn't a valid ECDSA key{/$}")
}
ErrorKind::InvalidToken => bunt::println!("{$red+bold}The JWT provided is invalid{/$}"),
ErrorKind::InvalidSignature => bunt::eprintln!("{$red+bold}The JWT provided has an invalid signature{/$}"),
ErrorKind::InvalidEcdsaKey => bunt::eprintln!("{$red+bold}The secret provided isn't a valid ECDSA key{/$}"),
ErrorKind::InvalidRsaKey(_) => bunt::eprintln!("{$red+bold}The secret provided isn't a valid RSA key{/$}"),
ErrorKind::MissingRequiredClaim(missing) => {
if missing.as_str() == "exp" {
bunt::eprintln!("{$red+bold}`exp` is missing, but is required. This error can be ignored via the `--ignore-exp` parameter.{/$}")
} else {
bunt::eprintln!("{$red+bold}`{:?}` is missing, but is required{/$}", missing)
}
}
ErrorKind::ExpiredSignature => {
bunt::eprintln!("{$red+bold}The token has expired (or the `exp` claim is not set). This error can be ignored via the `--ignore-exp` parameter.{/$}")
}
ErrorKind::InvalidIssuer => {
bunt::println!("{$red+bold}The token issuer is invalid{/$}")
}
ErrorKind::InvalidAudience => {
bunt::eprintln!("{$red+bold}The token audience doesn't match the subject{/$}")
}
ErrorKind::InvalidSubject => {
bunt::eprintln!("{$red+bold}The token subject doesn't match the audience{/$}")
}
ErrorKind::ExpiredSignature => bunt::eprintln!("{$red+bold}The token has expired (or the `exp` claim is not set). This error can be ignored via the `--ignore-exp` parameter.{/$}"),
ErrorKind::InvalidIssuer => bunt::println!("{$red+bold}The token issuer is invalid{/$}"),
ErrorKind::InvalidAudience => bunt::eprintln!("{$red+bold}The token audience doesn't match the subject{/$}"),
ErrorKind::InvalidSubject => bunt::eprintln!("{$red+bold}The token subject doesn't match the audience{/$}"),
ErrorKind::ImmatureSignature => bunt::eprintln!(
"{$red+bold}The `nbf` claim is in the future which isn't allowed{/$}"
),
ErrorKind::InvalidAlgorithm => bunt::eprintln!(
"{$red+bold}The JWT provided has a different signing algorithm than the one you \
provided{/$}",
),
ErrorKind::InvalidAlgorithmName => bunt::eprintln!(
"{$red+bold}The JWT provided has a different signing algorithm than the one you \
provided{/$}",
),
ErrorKind::InvalidKeyFormat => bunt::eprintln!("{$red+bold}The key provided is an invalid format{/$}"),
_ => bunt::eprintln!(
"{$red+bold}The JWT provided is invalid because{/$} {:?}",
"{$red+bold}The JWT provided is invalid because{/$} {}",
err
),
};
Expand Down

0 comments on commit 3becd97

Please sign in to comment.