1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Error type for the crate.
use thiserror::Error;
/// Errors that can occur while recording or querying audits.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AuditError {
/// An `audit_comment` was required for this action but none was supplied.
///
/// Some actions can be configured to require an `audit_comment`; this error is raised when
/// such a comment is mandatory but missing. For `update`
/// and `destroy` (which are audited *before* the database write) you can use this error to
/// abort the operation. For `create` (audited *after* the write) the row already exists, so
/// prefer pre-checking with [`crate::Auditable::comment_required_for`].
#[error("audit comment required for `{action}` but none was supplied")]
CommentRequired {
/// The action that required the comment.
action: crate::Action,
},
/// `undo` was called on an audit with an unrecognized action string.
#[error("invalid action given `{0}`")]
InvalidAction(String),
/// The persistence backend failed.
#[error("audit backend error: {0}")]
Backend(#[source] Box<dyn std::error::Error + Send + Sync>),
/// (De)serialization of `audited_changes` failed.
#[error("failed to (de)serialize audited_changes: {0}")]
Serialization(#[source] serde_json::Error),
}
impl AuditError {
/// Wrap an arbitrary backend error.
pub fn backend<E>(err: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
AuditError::Backend(Box::new(err))
}
}
/// Convenience alias used throughout the crate.
pub type Result<T> = std::result::Result<T, AuditError>;