sqrl_protocol/
error.rs

1//! A common error used by SQRL clients and servers
2
3use std::{fmt, num::ParseIntError, string::FromUtf8Error};
4
5/// An error that can occur during SQRL protocol
6pub struct SqrlError {
7    error_message: String,
8}
9
10impl SqrlError {
11    /// Create a new SqrlError with the string as error message
12    pub fn new(error: String) -> Self {
13        SqrlError {
14            error_message: error,
15        }
16    }
17}
18
19impl fmt::Display for SqrlError {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        write!(f, "{}", self.error_message)
22    }
23}
24
25impl fmt::Debug for SqrlError {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        write!(f, "{}", self.error_message)
28    }
29}
30
31impl std::error::Error for SqrlError {}
32
33impl From<url::ParseError> for SqrlError {
34    fn from(error: url::ParseError) -> Self {
35        SqrlError::new(error.to_string())
36    }
37}
38
39impl From<base64::DecodeError> for SqrlError {
40    fn from(error: base64::DecodeError) -> Self {
41        SqrlError::new(error.to_string())
42    }
43}
44
45impl From<FromUtf8Error> for SqrlError {
46    fn from(error: FromUtf8Error) -> Self {
47        SqrlError::new(error.to_string())
48    }
49}
50
51impl From<ParseIntError> for SqrlError {
52    fn from(value: ParseIntError) -> Self {
53        SqrlError::new(value.to_string())
54    }
55}