Skip to content

Commit 84eac53

Browse files
committed
Convert AuthError to Error<AuthErrorKind>
The only change here is that the `AuthError` became a specialization of the `error::Error<T>` generic. The error messages are moved to the implementation of its kind `AuthErrorKind`.
1 parent 69ee1bf commit 84eac53

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

async-nats/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ where
7373

7474
/// Enables wrapping source errors to the crate-specific error type
7575
/// by additionally specifying the kind of the target error.
76-
trait WithKind<Kind>
76+
pub(crate) trait WithKind<Kind>
7777
where
7878
Kind: Clone + Debug + Display + PartialEq,
7979
Self: Into<crate::Error>,

async-nats/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ mod options;
171171

172172
pub use auth::Auth;
173173
pub use client::{Client, PublishError, Request, RequestError, RequestErrorKind, SubscribeError};
174-
pub use options::{AuthError, ConnectOptions};
174+
pub use options::{AuthError, AuthErrorKind, ConnectOptions};
175175

176176
pub mod error;
177177
pub mod header;
@@ -881,6 +881,12 @@ impl From<io::Error> for ConnectError {
881881
}
882882
}
883883

884+
impl From<AuthError> for ConnectError {
885+
fn from(value: AuthError) -> Self {
886+
value.with_kind(ConnectErrorKind::Authentication)
887+
}
888+
}
889+
884890
/// Retrieves messages from given `subscription` created by [Client::subscribe].
885891
///
886892
/// Implements [futures::stream::Stream] for ergonomic async message processing.
@@ -1313,6 +1319,7 @@ macro_rules! from_with_timeout {
13131319
}
13141320
};
13151321
}
1322+
use crate::error::WithKind;
13161323
pub(crate) use from_with_timeout;
13171324

13181325
#[cfg(test)]

async-nats/src/options.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
use crate::auth::Auth;
1515
use crate::connector;
16+
use crate::error::Error;
1617
use crate::{Client, ConnectError, Event, ToServerAddrs};
1718
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
1819
use base64::engine::Engine;
1920
use futures::Future;
20-
use std::fmt::Formatter;
21+
use std::fmt::{Display, Formatter};
2122
use std::{
2223
fmt,
2324
path::{Path, PathBuf},
@@ -340,7 +341,7 @@ impl ConnectOptions {
340341
/// let jwt = load_jwt().await?;
341342
/// let nc = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| {
342343
/// let key_pair = key_pair.clone();
343-
/// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
344+
/// async move { key_pair.sign(&nonce) }
344345
/// })
345346
/// .connect("localhost")
346347
/// .await?;
@@ -374,7 +375,7 @@ impl ConnectOptions {
374375
/// let nc = async_nats::ConnectOptions::new()
375376
/// .jwt(jwt, move |nonce| {
376377
/// let key_pair = key_pair.clone();
377-
/// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
378+
/// async move { key_pair.sign(&nonce) }
378379
/// })
379380
/// .connect("localhost")
380381
/// .await?;
@@ -393,7 +394,7 @@ impl ConnectOptions {
393394
Box::pin(async move {
394395
let sig = sign_cb(nonce.as_bytes().to_vec())
395396
.await
396-
.map_err(AuthError::new)?;
397+
.map_err(|_| AuthError::new(AuthErrorKind::InvalidSignature))?;
397398
Ok(URL_SAFE_NO_PAD.encode(sig))
398399
})
399400
}));
@@ -509,7 +510,11 @@ impl ConnectOptions {
509510

510511
Ok(self.jwt(jwt.to_owned(), move |nonce| {
511512
let key_pair = key_pair.clone();
512-
async move { key_pair.sign(&nonce).map_err(AuthError::new) }
513+
async move {
514+
key_pair
515+
.sign(&nonce)
516+
.map_err(|_| AuthErrorKind::InvalidKeyPair.into())
517+
}
513518
}))
514519
}
515520

@@ -923,27 +928,20 @@ impl<A, T> fmt::Debug for CallbackArg1<A, T> {
923928
}
924929
}
925930

926-
/// Error report from signing callback.
927-
// This was needed because std::io::Error isn't Send.
928-
#[derive(Clone, PartialEq)]
929-
pub struct AuthError(String);
930-
931-
impl AuthError {
932-
pub fn new(s: impl ToString) -> Self {
933-
Self(s.to_string())
934-
}
931+
#[derive(Clone, Copy, Debug, PartialEq)]
932+
pub enum AuthErrorKind {
933+
InvalidKeyPair,
934+
InvalidSignature,
935935
}
936936

937-
impl std::fmt::Display for AuthError {
937+
impl Display for AuthErrorKind {
938938
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
939-
f.write_str(&format!("AuthError({})", &self.0))
940-
}
941-
}
942-
943-
impl std::fmt::Debug for AuthError {
944-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
945-
f.write_str(&format!("AuthError({})", &self.0))
939+
match self {
940+
Self::InvalidKeyPair => f.write_str("invalid keypair"),
941+
Self::InvalidSignature => f.write_str("invalid signature"),
942+
}
946943
}
947944
}
948945

949-
impl std::error::Error for AuthError {}
946+
/// Error report from signing callback.
947+
pub type AuthError = Error<AuthErrorKind>;

0 commit comments

Comments
 (0)