Skip to content

Commit f94e6a6

Browse files
committed
Added new succinct Exception classes
1 parent b4e0f8b commit f94e6a6

8 files changed

Lines changed: 42 additions & 35 deletions

SmartHealthCard.Token/Exceptions/DeserializationException.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SmartHealthCard.Token.Exceptions
2+
{
3+
public class SmartHealthCardDecoderException : SmartHealthCardException
4+
{
5+
public SmartHealthCardDecoderException(string Message)
6+
: base(Message){ }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SmartHealthCard.Token.Exceptions
2+
{
3+
public class SmartHealthCardEncoderException : SmartHealthCardException
4+
{
5+
public SmartHealthCardEncoderException(string Message)
6+
: base(Message){ }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SmartHealthCard.Token.Exceptions
2+
{
3+
public class SmartHealthCardJwksException : SmartHealthCardException
4+
{
5+
public SmartHealthCardJwksException(string Message)
6+
: base(Message){ }
7+
}
8+
}

SmartHealthCard.Token/Serializers/Shc/SmartHealthCardJwsPayloadSerializer.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ public async Task<Result<T>> DeserializeAsync<T>(byte[] bytes)
4545
Result<T> SmartHealthCardModelResult = FromJson<T>(MinifiedSmartHealthCardJson);
4646
if (SmartHealthCardModelResult.Failure)
4747
return Result<T>.Fail(SmartHealthCardModelResult.ErrorMessage);
48-
49-
return await Task.Run(() => FromJson<T>(MinifiedSmartHealthCardJson));
50-
//return (T)(object)SmartHealthCardModel;
48+
return await Task.Run(() => FromJson<T>(MinifiedSmartHealthCardJson));
5149
}
5250
else if (typeof(T) == typeof(JwsBody))
5351
{
@@ -56,16 +54,12 @@ public async Task<Result<T>> DeserializeAsync<T>(byte[] bytes)
5654
Result<T> JwsBodyResult = FromJson<T>(MinifiedSmartHealthCardJson);
5755
if (JwsBodyResult.Failure)
5856
return Result<T>.Fail(JwsBodyResult.ErrorMessage);
59-
6057
return await Task.Run(() => FromJson<T>(MinifiedSmartHealthCardJson));
61-
62-
//return (T)(object)FromJson<JwsBody>(MinifiedSmartHealthCardJson);
6358
}
6459
else
6560
{
6661
throw new TypeAccessException(typeof(T).Name);
6762
}
68-
6963
}
7064

7165
public Result<string> ToJson<T>(T Obj, bool Minified = true) => JsonSerializer.ToJson(Obj);

SmartHealthCard.Token/SmartHealthCardDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using SmartHealthCard.Token.Algorithms;
2+
using SmartHealthCard.Token.Exceptions;
23
using SmartHealthCard.Token.JwsToken;
34
using SmartHealthCard.Token.Model.Jwks;
45
using SmartHealthCard.Token.Model.Shc;
@@ -98,7 +99,7 @@ public async Task<string> DecodeToJsonAsync(string Token, bool Verify = true)
9899
SmartHealthCardModel SmartHealthCardModel = await DecodeAsync(Token, Verify);
99100
Result<string> ToJsonResult = JsonSerializer.ToJson(SmartHealthCardModel, Minified: false);
100101
if (ToJsonResult.Failure)
101-
throw new System.Exception(ToJsonResult.ErrorMessage);
102+
throw new SmartHealthCardDecoderException(ToJsonResult.ErrorMessage);
102103

103104
return ToJsonResult.Value;
104105
}
@@ -123,7 +124,7 @@ public async Task<SmartHealthCardModel> DecodeAsync(string Token, bool Verify =
123124
this.JwsPayloadValidator);
124125
Result<SmartHealthCardModel> DecodePayloadResult = await JwsDecoder.DecodePayloadAsync<SmartHealthCareJWSHeaderModel, SmartHealthCardModel>(Token: Token, Verity: Verify);
125126
if (DecodePayloadResult.Failure)
126-
throw new System.Exception(DecodePayloadResult.ErrorMessage);
127+
throw new SmartHealthCardDecoderException(DecodePayloadResult.ErrorMessage);
127128

128129
return DecodePayloadResult.Value;
129130
}
@@ -135,7 +136,7 @@ public async Task<SmartHealthCardModel> DecodeAsync(string Token, bool Verify =
135136

136137
Result<SmartHealthCardModel> DecodePayloadResult = await JwsDecoder.DecodePayloadAsync<SmartHealthCareJWSHeaderModel, SmartHealthCardModel>(Token: Token, Verity: Verify);
137138
if (DecodePayloadResult.Failure)
138-
throw new System.Exception(DecodePayloadResult.ErrorMessage);
139+
throw new SmartHealthCardDecoderException(DecodePayloadResult.ErrorMessage);
139140

140141
return DecodePayloadResult.Value;
141142
}

SmartHealthCard.Token/SmartHealthCardEncoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Security.Cryptography.X509Certificates;
99
using System.Threading.Tasks;
1010
using SmartHealthCard.Token.Support;
11+
using SmartHealthCard.Token.Exceptions;
1112

1213
namespace SmartHealthCard.Token
1314
{
@@ -62,7 +63,7 @@ public async Task<string> GetTokenAsync(X509Certificate2 Certificate, SmartHealt
6263
IJwsEncoder JwsEncoder = GetEncoder(Certificate, Algorithm);
6364
Result<string> EncoderResult = await JwsEncoder.EncodeAsync(Header, SmartHealthCard);
6465
if (EncoderResult.Failure)
65-
throw new System.Exception(EncoderResult.ErrorMessage);
66+
throw new SmartHealthCardEncoderException(EncoderResult.ErrorMessage);
6667
return EncoderResult.Value;
6768
}
6869

@@ -89,12 +90,11 @@ public async Task<string> GetSmartHealthCardFile(X509Certificate2 Certificate, L
8990
if (EncoderResult.Success)
9091
SmartHealthCardFile.VerifiableCredentialList.Add(EncoderResult.Value);
9192
else
92-
throw new System.Exception(EncoderResult.ErrorMessage);
93+
throw new SmartHealthCardEncoderException(EncoderResult.ErrorMessage);
9394
}
9495
Result<string> ToJsonResult = JsonSerializer.ToJson(SmartHealthCardFile);
9596
if (ToJsonResult.Failure)
96-
throw new System.Exception(ToJsonResult.ErrorMessage);
97-
97+
throw new SmartHealthCardEncoderException(ToJsonResult.ErrorMessage);
9898

9999
return ToJsonResult.Value;
100100
}
@@ -109,7 +109,7 @@ private SmartHealthCareJWSHeaderModel GetHeader(IAlgorithm Algorithm)
109109
{
110110
Result<string> KidResult = Algorithm.GetKid();
111111
if (KidResult.Failure)
112-
throw new System.Exception(KidResult.ErrorMessage);
112+
throw new SmartHealthCardEncoderException(KidResult.ErrorMessage);
113113

114114
//Create the Smart Health Card JWS Header Model
115115
return new SmartHealthCareJWSHeaderModel(Algorithm.Name, "DEF", KidResult.Value);

SmartHealthCard.Token/SmartHealthCardJWKS.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using SmartHealthCard.Token.Algorithms;
2+
using SmartHealthCard.Token.Exceptions;
23
using SmartHealthCard.Token.Model.Jwks;
34
using SmartHealthCard.Token.Serializers.Json;
45
using SmartHealthCard.Token.Support;
@@ -45,17 +46,12 @@ public JsonWebKeySet GetJsonWebKeySet(IEnumerable<X509Certificate2> CertificateL
4546
foreach (X509Certificate2 Certificate in CertificateList)
4647
{
4748
ES256Algorithm Algorithm = new ES256Algorithm(Certificate, JsonSerializer);
48-
Result<string> KidResult = Algorithm.GetKid();
49-
if (KidResult.Failure)
50-
throw new System.Exception(KidResult.ErrorMessage);
51-
52-
Result<string> XResult = Algorithm.GetPointCoordinateX();
53-
if (XResult.Failure)
54-
throw new System.Exception(XResult.ErrorMessage);
55-
56-
Result<string> YResult = Algorithm.GetPointCoordinateY();
57-
if (YResult.Failure)
58-
throw new System.Exception(YResult.ErrorMessage);
49+
Result<string> KidResult = Algorithm.GetKid();
50+
Result<string> XResult = Algorithm.GetPointCoordinateX();
51+
Result<string> YResult = Algorithm.GetPointCoordinateY();
52+
Result ResultCombine = Result.Combine(KidResult, XResult, YResult);
53+
if (ResultCombine.Failure)
54+
throw new SmartHealthCardJwksException(ResultCombine.ErrorMessage);
5955

6056
JsonWebKey JsonWebKeySetModel = new JsonWebKey(
6157
Kty: Algorithm.KeyTypeName,
@@ -83,7 +79,7 @@ public string Get(IEnumerable<X509Certificate2> CertificateList, bool Minified =
8379
IJsonSerializer JsonSerializer = new JsonSerializer();
8480
Result<string> ToJsonResult = JsonSerializer.ToJson(JsonWebKeySet, Minified);
8581
if (ToJsonResult.Failure)
86-
throw new System.Exception(ToJsonResult.ErrorMessage);
82+
throw new SmartHealthCardJwksException(ToJsonResult.ErrorMessage);
8783

8884
return ToJsonResult.Value;
8985
}

0 commit comments

Comments
 (0)