1use std::error::Error as StdError;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
6pub enum TokenError {
7 #[error("Biscuit token error: {0}")]
9 BiscuitError(String),
10
11 #[error("Token verification error: {0}")]
13 VerificationError(String),
14
15 #[error("Invalid key format: {0}")]
17 InvalidKeyFormat(String),
18
19 #[error("Authorization failed: {0}")]
21 AuthorizationError(String),
22
23 #[error("{0}")]
25 Generic(String),
26}
27
28impl TokenError {
29 pub fn biscuit_error<E: StdError>(err: E) -> Self {
31 TokenError::BiscuitError(err.to_string())
32 }
33
34 pub fn verification_error<S: Into<String>>(msg: S) -> Self {
36 TokenError::VerificationError(msg.into())
37 }
38
39 pub fn invalid_key_format<S: Into<String>>(msg: S) -> Self {
41 TokenError::InvalidKeyFormat(msg.into())
42 }
43
44 pub fn authorization_error<S: Into<String>>(msg: S) -> Self {
46 TokenError::AuthorizationError(msg.into())
47 }
48
49 pub fn generic<S: Into<String>>(msg: S) -> Self {
51 TokenError::Generic(msg.into())
52 }
53}
54
55impl From<biscuit_auth::error::Token> for TokenError {
56 fn from(err: biscuit_auth::error::Token) -> Self {
57 TokenError::BiscuitError(err.to_string())
58 }
59}
60
61impl From<hex::FromHexError> for TokenError {
62 fn from(err: hex::FromHexError) -> Self {
63 TokenError::InvalidKeyFormat(err.to_string())
64 }
65}
66
67impl From<&str> for TokenError {
68 fn from(err: &str) -> Self {
69 TokenError::Generic(err.to_string())
70 }
71}
72
73impl From<String> for TokenError {
74 fn from(err: String) -> Self {
75 TokenError::Generic(err)
76 }
77}