Skip to content

Commit

Permalink
errors: display more info about failed checks and policies
Browse files Browse the repository at this point in the history
  • Loading branch information
divarvel committed Nov 25, 2024
1 parent 6ad6f12 commit b62be7a
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions biscuit-auth/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! error types
//!
use std::convert::{From, Infallible};
use std::{
convert::{From, Infallible},
fmt::Display,
};
use thiserror::Error;

/// the global error type for Biscuit
Expand All @@ -16,7 +19,7 @@ pub enum Token {
AppendOnSealed,
#[error("tried to seal an already sealed token")]
AlreadySealed,
#[error("authorization failed")]
#[error("authorization failed: {0}")]
FailedLogic(Logic),
#[error("error generating Datalog: {0}")]
Language(biscuit_parser::error::LanguageError),
Expand Down Expand Up @@ -170,7 +173,7 @@ pub enum Signature {
pub enum Logic {
#[error("a rule provided by a block is generating facts with the authority or ambient tag, or has head variables not used in its body")]
InvalidBlockRule(u32, String),
#[error("authorization failed")]
#[error("{policy}, and the following checks failed: {checks:?}")]
Unauthorized {
/// the policy that matched
policy: MatchedPolicy,
Expand All @@ -179,7 +182,7 @@ pub enum Logic {
},
#[error("the authorizer already contains a token")]
AuthorizerNotEmpty,
#[error("no matching policy was found")]
#[error("no matching policy was found, and the following checks failed: {checks:?}")]
NoMatchingPolicy {
/// list of checks that failed validation
checks: Vec<FailedCheck>,
Expand All @@ -189,19 +192,19 @@ pub enum Logic {
#[derive(Error, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum MatchedPolicy {
#[error("an allow policy matched")]
#[error("an allow policy matched (policy index: {0})")]
Allow(usize),
#[error("a deny policy matched")]
#[error("a deny policy matched (policy index: {0})")]
Deny(usize),
}

/// check errors
#[derive(Error, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum FailedCheck {
#[error("a check failed in a block")]
#[error("a check failed in a block: {0}")]
Block(FailedBlockCheck),
#[error("a check provided by the authorizer failed")]
#[error("a check provided by the authorizer failed: {0}")]
Authorizer(FailedAuthorizerCheck),
}

Expand All @@ -214,6 +217,16 @@ pub struct FailedBlockCheck {
pub rule: String,
}

impl Display for FailedBlockCheck {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Check n°{} in block n°{}: {}",
self.check_id, self.block_id, self.rule
)
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub struct FailedAuthorizerCheck {
Expand All @@ -222,6 +235,12 @@ pub struct FailedAuthorizerCheck {
pub rule: String,
}

impl Display for FailedAuthorizerCheck {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Check n°{} in authorizer: {}", self.check_id, self.rule)
}
}

/// Datalog execution errors
#[derive(Error, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
Expand Down

0 comments on commit b62be7a

Please sign in to comment.