pub struct Denial { /* private fields */ }Expand description
A refusal from a gate: what the visitor sees, and a stable code for logs and events (never shown to the visitor).
use web_net::Denial;
let d = Denial::forbidden("admin.only");
assert_eq!((d.status(), d.code()), (403, "admin.only"));
assert_eq!(d.to_string(), "denied (403): admin.only");Implementations§
Source§impl Denial
impl Denial
Sourcepub const fn sign_in(code: &'static str) -> Self
pub const fn sign_in(code: &'static str) -> Self
Sign in first.
use web_net::{Denial, DenialKind};
let d = Denial::sign_in("session.none");
assert_eq!((d.kind(), d.status()), (DenialKind::SignIn, 401));Sourcepub const fn forbidden(code: &'static str) -> Self
pub const fn forbidden(code: &'static str) -> Self
403 Forbidden.
use web_net::Denial;
assert_eq!(Denial::forbidden("account.locked").status(), 403);404 Not Found, hiding that the resource exists.
use web_net::Denial;
assert_eq!(Denial::hidden("post.draft").status(), 404);Sourcepub const fn with_status(status: u16, code: &'static str) -> Self
pub const fn with_status(status: u16, code: &'static str) -> Self
Another client-error status (429, 451, …). A status outside 400 to 499 is answered as 403: a gate cannot turn a denial into a success or a server error.
use web_net::Denial;
assert_eq!(Denial::with_status(429, "rate.limited").status(), 429);
assert_eq!(Denial::with_status(500, "oops").status(), 403);503 Service Unavailable: the gate cannot decide now (a store it needs is down) and fails closed. Clients and load balancers read it as “try again”, not “you may not”.
use web_net::Denial;
let d = Denial::unavailable("gate.unavailable").retry_after(5);
assert_eq!((d.status(), d.retry_seconds()), (503, Some(5)));Sourcepub const fn retry_after(self, seconds: u32) -> Self
pub const fn retry_after(self, seconds: u32) -> Self
A Retry-After: <seconds> field on the refusal. Sent only when the
status is 429 or 503, the statuses a client retries.
use web_net::Denial;
let d = Denial::with_status(429, "rate.limited").retry_after(30);
assert_eq!(d.retry_seconds(), Some(30));Sourcepub const fn retry_seconds(&self) -> Option<u32>
pub const fn retry_seconds(&self) -> Option<u32>
The Retry-After seconds, if any.
use web_net::Denial;
assert_eq!(Denial::forbidden("account.locked").retry_seconds(), None);
assert_eq!(Denial::unavailable("store.down").retry_after(5).retry_seconds(), Some(5));A Set-Cookie value the refusal carries (a dead session’s cookie
cleared): written on the denial’s reply.
use web_net::Denial;
let d = Denial::sign_in("session.expired").with_cookie("sid=; Max-Age=0; Path=/");
assert_eq!(d.cookie(), Some("sid=; Max-Age=0; Path=/"));The Set-Cookie value, if any.
use web_net::Denial;
assert_eq!(Denial::forbidden("account.locked").cookie(), None);
assert!(Denial::sign_in("session.expired").with_cookie("sid=; Max-Age=0").cookie().is_some());Sourcepub const fn kind(&self) -> DenialKind
pub const fn kind(&self) -> DenialKind
What the visitor is told.
use web_net::{Denial, DenialKind};
assert_eq!(Denial::unavailable("store.down").kind(), DenialKind::Unavailable);Trait Implementations§
impl Eq for Denial
Source§impl Error for Denial
impl Error for Denial
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()