From b74afb3cc8a52f658d3fe76cebff9ec1619fd84b Mon Sep 17 00:00:00 2001 From: Andrew Kozin Date: Fri, 4 Aug 2023 21:13:24 +0200 Subject: [PATCH] Convert AuthError to Error The only change here is that the `AuthError` became a specialization of the `error::Error` generic. The error messages are moved to the implementation of its kind `AuthErrorKind`. --- async-nats/src/error.rs | 2 +- async-nats/src/lib.rs | 9 +++++++- async-nats/src/options.rs | 44 +++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/async-nats/src/error.rs b/async-nats/src/error.rs index 13cd633af..567143723 100644 --- a/async-nats/src/error.rs +++ b/async-nats/src/error.rs @@ -73,7 +73,7 @@ where /// Enables wrapping source errors to the crate-specific error type /// by additionally specifying the kind of the target error. -trait WithKind +pub(crate) trait WithKind where Kind: Clone + Debug + Display + PartialEq, Self: Into, diff --git a/async-nats/src/lib.rs b/async-nats/src/lib.rs index 101b2eced..95821227a 100644 --- a/async-nats/src/lib.rs +++ b/async-nats/src/lib.rs @@ -174,7 +174,7 @@ mod options; pub use auth::Auth; pub use client::{Client, PublishError, Request, RequestError, RequestErrorKind, SubscribeError}; -pub use options::{AuthError, ConnectOptions}; +pub use options::{AuthError, AuthErrorKind, ConnectOptions}; pub mod error; pub mod header; @@ -982,6 +982,12 @@ impl From for ConnectError { } } +impl From for ConnectError { + fn from(value: AuthError) -> Self { + value.with_kind(ConnectErrorKind::Authentication) + } +} + /// Retrieves messages from given `subscription` created by [Client::subscribe]. /// /// Implements [futures::stream::Stream] for ergonomic async message processing. @@ -1413,6 +1419,7 @@ macro_rules! from_with_timeout { } }; } +use crate::error::WithKind; pub(crate) use from_with_timeout; #[cfg(test)] diff --git a/async-nats/src/options.rs b/async-nats/src/options.rs index 89132f96b..a25b10fb0 100644 --- a/async-nats/src/options.rs +++ b/async-nats/src/options.rs @@ -13,11 +13,12 @@ use crate::auth::Auth; use crate::connector; +use crate::error::Error; use crate::{Client, ConnectError, Event, ToServerAddrs}; use base64::engine::general_purpose::URL_SAFE_NO_PAD; use base64::engine::Engine; use futures::Future; -use std::fmt::Formatter; +use std::fmt::{Display, Formatter}; use std::{ fmt, path::{Path, PathBuf}, @@ -337,7 +338,7 @@ impl ConnectOptions { /// let jwt = load_jwt().await?; /// let nc = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| { /// let key_pair = key_pair.clone(); - /// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) } + /// async move { key_pair.sign(&nonce) } /// }) /// .connect("localhost") /// .await?; @@ -371,7 +372,7 @@ impl ConnectOptions { /// let nc = async_nats::ConnectOptions::new() /// .jwt(jwt, move |nonce| { /// let key_pair = key_pair.clone(); - /// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) } + /// async move { key_pair.sign(&nonce) } /// }) /// .connect("localhost") /// .await?; @@ -390,7 +391,7 @@ impl ConnectOptions { Box::pin(async move { let sig = sign_cb(nonce.as_bytes().to_vec()) .await - .map_err(AuthError::new)?; + .map_err(|_| AuthError::new(AuthErrorKind::InvalidSignature))?; Ok(URL_SAFE_NO_PAD.encode(sig)) }) })); @@ -506,7 +507,11 @@ impl ConnectOptions { Ok(self.jwt(jwt.to_owned(), move |nonce| { let key_pair = key_pair.clone(); - async move { key_pair.sign(&nonce).map_err(AuthError::new) } + async move { + key_pair + .sign(&nonce) + .map_err(|_| AuthErrorKind::InvalidKeyPair.into()) + } })) } @@ -899,27 +904,20 @@ impl fmt::Debug for CallbackArg1 { } } -/// Error report from signing callback. -// This was needed because std::io::Error isn't Send. -#[derive(Clone, PartialEq)] -pub struct AuthError(String); - -impl AuthError { - pub fn new(s: impl ToString) -> Self { - Self(s.to_string()) - } +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum AuthErrorKind { + InvalidKeyPair, + InvalidSignature, } -impl std::fmt::Display for AuthError { +impl Display for AuthErrorKind { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.write_str(&format!("AuthError({})", &self.0)) - } -} - -impl std::fmt::Debug for AuthError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.write_str(&format!("AuthError({})", &self.0)) + match self { + Self::InvalidKeyPair => f.write_str("invalid keypair"), + Self::InvalidSignature => f.write_str("invalid signature"), + } } } -impl std::error::Error for AuthError {} +/// Error report from signing callback. +pub type AuthError = Error;