Skip to content

Commit b928510

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 02fd086 commit b928510

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
@@ -174,7 +174,7 @@ mod options;
174174

175175
pub use auth::Auth;
176176
pub use client::{Client, PublishError, Request, RequestError, RequestErrorKind, SubscribeError};
177-
pub use options::{AuthError, ConnectOptions};
177+
pub use options::{AuthError, AuthErrorKind, ConnectOptions};
178178

179179
pub mod error;
180180
pub mod header;
@@ -987,6 +987,12 @@ impl From<io::Error> for ConnectError {
987987
}
988988
}
989989

990+
impl From<AuthError> for ConnectError {
991+
fn from(value: AuthError) -> Self {
992+
value.with_kind(ConnectErrorKind::Authentication)
993+
}
994+
}
995+
990996
/// Retrieves messages from given `subscription` created by [Client::subscribe].
991997
///
992998
/// Implements [futures::stream::Stream] for ergonomic async message processing.
@@ -1418,6 +1424,7 @@ macro_rules! from_with_timeout {
14181424
}
14191425
};
14201426
}
1427+
use crate::error::WithKind;
14211428
pub(crate) use from_with_timeout;
14221429

14231430
#[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},
@@ -337,7 +338,7 @@ impl ConnectOptions {
337338
/// let jwt = load_jwt().await?;
338339
/// let nc = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| {
339340
/// let key_pair = key_pair.clone();
340-
/// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
341+
/// async move { key_pair.sign(&nonce) }
341342
/// })
342343
/// .connect("localhost")
343344
/// .await?;
@@ -371,7 +372,7 @@ impl ConnectOptions {
371372
/// let nc = async_nats::ConnectOptions::new()
372373
/// .jwt(jwt, move |nonce| {
373374
/// let key_pair = key_pair.clone();
374-
/// async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
375+
/// async move { key_pair.sign(&nonce) }
375376
/// })
376377
/// .connect("localhost")
377378
/// .await?;
@@ -390,7 +391,7 @@ impl ConnectOptions {
390391
Box::pin(async move {
391392
let sig = sign_cb(nonce.as_bytes().to_vec())
392393
.await
393-
.map_err(AuthError::new)?;
394+
.map_err(|_| AuthError::new(AuthErrorKind::InvalidSignature))?;
394395
Ok(URL_SAFE_NO_PAD.encode(sig))
395396
})
396397
}));
@@ -506,7 +507,11 @@ impl ConnectOptions {
506507

507508
Ok(self.jwt(jwt.to_owned(), move |nonce| {
508509
let key_pair = key_pair.clone();
509-
async move { key_pair.sign(&nonce).map_err(AuthError::new) }
510+
async move {
511+
key_pair
512+
.sign(&nonce)
513+
.map_err(|_| AuthErrorKind::InvalidKeyPair.into())
514+
}
510515
}))
511516
}
512517

@@ -899,27 +904,20 @@ impl<A, T> fmt::Debug for CallbackArg1<A, T> {
899904
}
900905
}
901906

902-
/// Error report from signing callback.
903-
// This was needed because std::io::Error isn't Send.
904-
#[derive(Clone, PartialEq)]
905-
pub struct AuthError(String);
906-
907-
impl AuthError {
908-
pub fn new(s: impl ToString) -> Self {
909-
Self(s.to_string())
910-
}
907+
#[derive(Clone, Copy, Debug, PartialEq)]
908+
pub enum AuthErrorKind {
909+
InvalidKeyPair,
910+
InvalidSignature,
911911
}
912912

913-
impl std::fmt::Display for AuthError {
913+
impl Display for AuthErrorKind {
914914
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
915-
f.write_str(&format!("AuthError({})", &self.0))
916-
}
917-
}
918-
919-
impl std::fmt::Debug for AuthError {
920-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
921-
f.write_str(&format!("AuthError({})", &self.0))
915+
match self {
916+
Self::InvalidKeyPair => f.write_str("invalid keypair"),
917+
Self::InvalidSignature => f.write_str("invalid signature"),
918+
}
922919
}
923920
}
924921

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

0 commit comments

Comments
 (0)