Skip to content

Commit 01afa23

Browse files
committed
Use internal macros for error handling
1 parent 0e08c70 commit 01afa23

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

src/auth/authentication_scheme.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::{self, Display};
22
use std::str::FromStr;
33

4-
use crate::format_err;
4+
use crate::bail2 as bail;
55

66
/// HTTP Mutual Authentication Algorithms
77
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -64,11 +64,7 @@ impl FromStr for AuthenticationScheme {
6464
"scram-sha-1" => Ok(Self::ScramSha1),
6565
"scram-sha-256" => Ok(Self::ScramSha256),
6666
"vapid" => Ok(Self::Vapid),
67-
s => {
68-
let mut err = format_err!("`{}` is not a recognized authentication scheme", s);
69-
err.set_status(400);
70-
Err(err)
71-
}
67+
s => bail!(400, "`{}` is not a recognized authentication scheme", s),
7268
}
7369
}
7470
}

src/auth/authorization.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::auth::AuthenticationScheme;
2-
use crate::bail;
2+
use crate::bail2 as bail;
33
use crate::headers::{HeaderName, HeaderValue, Headers, AUTHORIZATION};
44

55
/// Credentials to authenticate a user agent with a server.
@@ -60,8 +60,8 @@ impl Authorization {
6060
let scheme = iter.next();
6161
let credential = iter.next();
6262
let (scheme, credentials) = match (scheme, credential) {
63-
(None, _) => bail!("Could not find scheme"),
64-
(Some(_), None) => bail!("Could not find credentials"),
63+
(None, _) => bail!(400, "Could not find scheme"),
64+
(Some(_), None) => bail!(400, "Could not find credentials"),
6565
(Some(scheme), Some(credentials)) => (scheme.parse()?, credentials.to_owned()),
6666
};
6767

src/auth/basic_auth.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::auth::{AuthenticationScheme, Authorization};
2-
use crate::format_err;
32
use crate::headers::{HeaderName, HeaderValue, Headers, AUTHORIZATION};
43
use crate::Status;
4+
use crate::{bail2 as bail, ensure2 as ensure};
55

66
/// HTTP Basic authorization.
77
///
@@ -57,11 +57,12 @@ impl BasicAuth {
5757
};
5858

5959
let scheme = auth.scheme();
60-
if !matches!(scheme, AuthenticationScheme::Basic) {
61-
let mut err = format_err!("Expected basic auth scheme found `{}`", scheme);
62-
err.set_status(400);
63-
return Err(err);
64-
}
60+
ensure!(
61+
matches!(scheme, AuthenticationScheme::Basic),
62+
400,
63+
"Expected basic auth scheme found `{}`",
64+
scheme
65+
);
6566

6667
let bytes = base64::decode(auth.credentials()).status(400)?;
6768
let credentials = String::from_utf8(bytes).status(400)?;
@@ -72,16 +73,8 @@ impl BasicAuth {
7273

7374
let (username, password) = match (username, password) {
7475
(Some(username), Some(password)) => (username.to_string(), password.to_string()),
75-
(Some(_), None) => {
76-
let mut err = format_err!("Expected basic auth to a password");
77-
err.set_status(400);
78-
return Err(err);
79-
}
80-
(None, _) => {
81-
let mut err = format_err!("Expected basic auth to contain a username");
82-
err.set_status(400);
83-
return Err(err);
84-
}
76+
(Some(_), None) => bail!(400, "Expected basic auth to a password"),
77+
(None, _) => bail!(400, "Expected basic auth to contain a username"),
8578
};
8679

8780
Ok(Some(Self { username, password }))

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ macro_rules! ensure2 {
122122
};
123123
($cond:expr, $status:literal, $msg:expr, $($arg:tt)*) => {
124124
if !$cond {
125-
return $crate::private::Err($crate::format_err2!($status, $fmt, $($arg)*));
125+
return $crate::private::Err($crate::format_err2!($status, $msg, $($arg)*));
126126
}
127127
};
128128
}
@@ -175,7 +175,7 @@ macro_rules! format_err2 {
175175
err
176176
}};
177177
($status:literal, $msg:expr, $($arg:tt)*) => {{
178-
let mut err = $crate::private::new_adhoc(format!($msg, $($arg)*))
178+
let mut err = $crate::private::new_adhoc(format!($msg, $($arg)*));
179179
err.set_status($status);
180180
err
181181
}};

0 commit comments

Comments
 (0)