pub struct AccessDenied { /* private fields */ }Expand description
Error returned when authorization is explicitly denied.
This type is distinct from SDK errors (Error). It represents a successful
authorization check that resulted in denial, not a failure to check.
§When is AccessDenied Returned?
check()returnsOk(false)for denial (not an error)require()returnsErr(AccessDenied)for denial
use inferadb::VaultClient;
async fn example(vault: &VaultClient) -> Result<(), Box<dyn std::error::Error>> {
// check() - denial is Ok(false), not an error
let allowed = vault.check("user:alice", "view", "doc:secret").await?;
if !allowed {
println!("Access denied (but no error)");
}
// require() - denial IS an error (AccessDenied)
vault.check("user:alice", "view", "doc:secret")
.require()
.await?; // Returns Err(AccessDenied) if denied
Ok(())
}§Key Invariant
AccessDenied is NOT the same as ErrorKind::Forbidden:
| Type | Meaning | Example |
|---|---|---|
AccessDenied | Subject lacks permission to resource | Alice can’t view doc:secret |
ErrorKind::Forbidden | API caller lacks control plane permission | Can’t manage vault |
§Rich Context
AccessDenied includes the authorization context for debugging:
use inferadb::AccessDenied;
fn handle_denied(denied: &AccessDenied) {
println!("Subject: {}", denied.subject());
println!("Permission: {}", denied.permission());
println!("Resource: {}", denied.resource());
if let Some(reason) = denied.reason() {
println!("Reason: {}", reason);
}
}Implementations§
Source§impl AccessDenied
impl AccessDenied
Sourcepub fn new(
subject: impl Into<Cow<'static, str>>,
permission: impl Into<Cow<'static, str>>,
resource: impl Into<Cow<'static, str>>,
) -> Self
pub fn new( subject: impl Into<Cow<'static, str>>, permission: impl Into<Cow<'static, str>>, resource: impl Into<Cow<'static, str>>, ) -> Self
Creates a new AccessDenied error.
§Arguments
subject- The subject (e.g., “user:alice”) that was deniedpermission- The permission (e.g., “view”) that was checkedresource- The resource (e.g., “document:readme”) that was checked
§Example
use inferadb::AccessDenied;
let denied = AccessDenied::new("user:alice", "delete", "document:readme");
assert_eq!(denied.subject(), "user:alice");
assert_eq!(denied.permission(), "delete");
assert_eq!(denied.resource(), "document:readme");Sourcepub fn subject(&self) -> &str
pub fn subject(&self) -> &str
Returns the subject that was denied access.
This is typically in the format “type:id”, e.g., “user:alice” or “team:engineering”.
Sourcepub fn permission(&self) -> &str
pub fn permission(&self) -> &str
Returns the permission that was checked.
For example: “view”, “edit”, “delete”, “admin”.
Sourcepub fn resource(&self) -> &str
pub fn resource(&self) -> &str
Returns the resource that was checked.
This is typically in the format “type:id”, e.g., “document:readme” or “folder:reports”.
Sourcepub fn reason(&self) -> Option<&str>
pub fn reason(&self) -> Option<&str>
Returns the denial reason, if available.
The reason provides additional context about why access was denied. This may include information about missing relationships or failed conditions.
Sourcepub fn request_id(&self) -> Option<&str>
pub fn request_id(&self) -> Option<&str>
Returns the request ID, if available.
Include this in logs for debugging and support correlation.
Sourcepub fn with_reason(self, reason: impl Into<Cow<'static, str>>) -> Self
pub fn with_reason(self, reason: impl Into<Cow<'static, str>>) -> Self
Sets the denial reason.
Sourcepub fn with_request_id(self, request_id: impl Into<String>) -> Self
pub fn with_request_id(self, request_id: impl Into<String>) -> Self
Sets the request ID.
Sourcepub fn to_log_string(&self) -> String
pub fn to_log_string(&self) -> String
Returns a formatted string suitable for logging.
This includes all available context in a structured format.
Trait Implementations§
Source§impl Clone for AccessDenied
impl Clone for AccessDenied
Source§fn clone(&self) -> AccessDenied
fn clone(&self) -> AccessDenied
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AccessDenied
impl Debug for AccessDenied
Source§impl Display for AccessDenied
impl Display for AccessDenied
Source§impl Error for AccessDenied
impl Error for AccessDenied
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
Source§impl From<AccessDenied> for Error
Allows converting AccessDenied to the main Error type.
impl From<AccessDenied> for Error
Allows converting AccessDenied to the main Error type.
Note: This creates an Error with kind Forbidden, but AccessDenied
and ErrorKind::Forbidden have different semantic meanings:
AccessDenied: Subject lacks permission (data plane)ErrorKind::Forbidden: Caller lacks API permission (control plane)
Source§fn from(denied: AccessDenied) -> Self
fn from(denied: AccessDenied) -> Self
Auto Trait Implementations§
impl Freeze for AccessDenied
impl RefUnwindSafe for AccessDenied
impl Send for AccessDenied
impl Sync for AccessDenied
impl Unpin for AccessDenied
impl UnwindSafe for AccessDenied
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.