jws/
error.rs

1//! Error types for this crate.
2
3use std::fmt;
4
5/// [`std::result::Result`] with the error type filled in.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Indicates the type of an error that can occur during JWS processing.
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10pub enum ErrorKind {
11	/// A required header parameter is missing.
12	MissingHeaderParam,
13
14	/// A header param was found but it's value is invalid.
15	InvalidHeaderParam,
16
17	/// The MAC algorithm indicates by the JWS header is not supported by the used [`crate::Verifier`].
18	UnsupportedMacAlgorithm,
19
20	/// The message being processed is not valid.
21	InvalidMessage,
22
23	/// The signature of a message being verified is invalid.
24	InvalidSignature,
25
26	/// An error that doesn't match any of the other types.
27	Other,
28}
29
30/// An error that can occur during JWS processing.
31///
32/// An error consists of an [`ErrorKind`] indicating the type of error,
33/// and a human readable message.
34///
35/// The message is purely for human consumption.
36/// It should not be used by error handling code to change handling logic.
37#[derive(Clone, Debug)]
38pub struct Error{
39	pub kind:    ErrorKind,
40	pub message: String,
41}
42
43impl ErrorKind {
44	fn with_message(self, message: impl Into<String>) -> Error {
45		Error{kind: self, message: message.into()}
46	}
47}
48
49impl Error {
50	#![allow(non_upper_case_globals)]
51
52	pub const Other                   : ErrorKind = ErrorKind::Other;
53	pub const MissingHeaderParam      : ErrorKind = ErrorKind::MissingHeaderParam;
54	pub const InvalidHeaderParam      : ErrorKind = ErrorKind::InvalidHeaderParam;
55	pub const UnsupportedMacAlgorithm : ErrorKind = ErrorKind::UnsupportedMacAlgorithm;
56	pub const InvalidMessage          : ErrorKind = ErrorKind::InvalidMessage;
57	pub const InvalidSignature        : ErrorKind = ErrorKind::InvalidSignature;
58
59	/// Get the kind of error.
60	pub fn kind(&self) -> ErrorKind {
61		self.kind
62	}
63
64	/// Get the error message.
65	pub fn message(&self) -> &str {
66		&self.message
67	}
68
69	/// Create a new error of type [`ErrorKind::Other`] with a given message.
70	pub fn other(message: impl Into<String>) -> Self {
71		ErrorKind::Other.with_message(message)
72	}
73
74	/// Create a new error of type [`ErrorKind::MissingHeaderParam`] with a given message.
75	pub fn missing_header_param(message: impl Into<String>) -> Self {
76		ErrorKind::MissingHeaderParam.with_message(message)
77	}
78
79	/// Create a new error of type [`ErrorKind::InvalidHeaderParam`] with a given message.
80	pub fn invalid_header_param(message: impl Into<String>) -> Self {
81		ErrorKind::InvalidHeaderParam.with_message(message)
82	}
83
84	/// Create a new error of type [`ErrorKind::UnsupportedMacAlgorithm`] with a given message.
85	pub fn unsupported_mac_algorithm(message: impl Into<String>) -> Self {
86		ErrorKind::UnsupportedMacAlgorithm.with_message(message)
87	}
88
89	/// Create a new error of type [`ErrorKind::InvalidMessage`] with a given message.
90	pub fn invalid_message(message: impl Into<String>) -> Self {
91		ErrorKind::InvalidMessage.with_message(message)
92	}
93
94	/// Create a new error of type [`ErrorKind::InvalidSignature`] with a given message.
95	pub fn invalid_signature(message: impl Into<String>) -> Self {
96		ErrorKind::InvalidSignature.with_message(message)
97	}
98}
99
100
101impl fmt::Display for ErrorKind {
102	fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
103		match self {
104			ErrorKind::Other                   => write!(formatter, "unspecified error"),
105			ErrorKind::MissingHeaderParam      => write!(formatter, "missing header parameter"),
106			ErrorKind::InvalidHeaderParam      => write!(formatter, "invalid header parameter"),
107			ErrorKind::UnsupportedMacAlgorithm => write!(formatter, "unsupported MAC algorithm"),
108			ErrorKind::InvalidMessage          => write!(formatter, "invalid message"),
109			ErrorKind::InvalidSignature        => write!(formatter, "invalid signature"),
110		}
111	}
112}
113
114impl fmt::Display for Error {
115	fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
116		if self.message.is_empty() {
117			write!(formatter, "{}", self.kind)
118		} else {
119			write!(formatter, "{}: {}", self.kind, self.message)
120		}
121	}
122}
123
124impl std::error::Error for Error {}