Skip to content

Commit cbb6fef

Browse files
committed
Create JsonWebKeyError
Signed-off-by: Patrik Stas <[email protected]>
1 parent b95c658 commit cbb6fef

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

did_core/did_doc/src/schema/types/jsonwebkey.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@ use std::{
33
fmt::{self, Display, Formatter},
44
str::FromStr,
55
};
6+
use std::error::Error;
67

78
use serde::{Deserialize, Serialize};
89
use serde_json::Value;
10+
use thiserror::Error;
11+
12+
#[derive(Debug, Error)]
13+
pub struct JsonWebKeyError {
14+
reason: &'static str,
15+
#[source]
16+
source: Box<dyn Error + Sync + Send>,
17+
}
18+
19+
impl Display for JsonWebKeyError {
20+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
21+
write!(
22+
f,
23+
"JsonWebKeyError, reason: {}, source: {}",
24+
self.reason, self.source
25+
)
26+
}
27+
}
928

1029
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
1130
// TODO: Introduce proper custom type
@@ -23,17 +42,23 @@ pub struct JsonWebKey {
2342

2443
impl JsonWebKey {
2544
// todo: More future-proof way would be creating custom error type, but seems as overkill atm?
26-
pub fn new(jwk: &str) -> Result<Self, serde_json::Error> {
27-
serde_json::from_str(jwk)
45+
pub fn new(jwk: &str) -> Result<Self, JsonWebKeyError> {
46+
serde_json::from_str(jwk).map_err(|err| JsonWebKeyError {
47+
reason: "Parsing JWK failed",
48+
source: Box::new(err),
49+
})
2850
}
2951

30-
pub fn to_vec(&self) -> Result<Vec<u8>, serde_json::Error> {
31-
serde_json::to_vec(self)
52+
pub fn to_vec(&self) -> Result<Vec<u8>, JsonWebKeyError> {
53+
serde_json::to_vec(self).map_err(|err| JsonWebKeyError {
54+
reason: "Serializing JWK to vector failed",
55+
source: Box::new(err),
56+
})
3257
}
3358
}
3459

3560
impl FromStr for JsonWebKey {
36-
type Err = serde_json::Error;
61+
type Err = JsonWebKeyError;
3762

3863
fn from_str(s: &str) -> Result<Self, Self::Err> {
3964
Self::new(s)

did_core/did_doc/src/schema/types/multibase.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use thiserror::Error;
1010

1111
#[derive(Debug, Error)]
1212
pub struct MultibaseWrapperError {
13-
reason: String,
13+
reason: &'static str,
1414
#[source]
1515
source: Box<dyn Error + Sync + Send>,
1616
}
@@ -35,7 +35,7 @@ pub struct Multibase {
3535
impl Multibase {
3636
pub fn new(multibase: String) -> Result<Self, MultibaseWrapperError> {
3737
let (base, bytes) = decode(multibase).map_err(|err| MultibaseWrapperError {
38-
reason: format!("Decoding multibase value failed"),
38+
reason: "Decoding multibase value failed",
3939
source: Box::new(err),
4040
})?;
4141
Ok(Self { base, bytes })
@@ -136,7 +136,7 @@ mod tests {
136136
#[test]
137137
fn test_multibase_from_str_invalid() {
138138
let multibase = "invalidmultibasekey".parse::<Multibase>();
139-
let err = multibase.err().expect("Error was expected.");
139+
let err = multibase.expect_err("Error was expected.");
140140
assert!(err
141141
.source()
142142
.expect("Error was expected to has source set up.")

did_core/did_doc/src/schema/verification_method/error.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use thiserror::Error;
2+
use crate::schema::types::jsonwebkey::JsonWebKeyError;
23

34
use crate::schema::types::multibase::MultibaseWrapperError;
45

@@ -17,7 +18,7 @@ pub enum KeyDecodingError {
1718
#[error("Hex decoding error: ${0}")]
1819
HexDecodeError(hex::FromHexError),
1920
#[error("Jwk decoding error: ${0}")]
20-
JwkDecodeError(serde_json::Error),
21+
JwkDecodeError(JsonWebKeyError),
2122
#[error("Multibase decoding error ${0}")]
2223
MultibaseError(MultibaseWrapperError),
2324
}

0 commit comments

Comments
 (0)