-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom exceptions for model binding errors
- Addds MissingClaimException that is now thrown when a claim can not be found - Adds ClaimParsingException that is thrown when a claim can not be parsed to the specified type
- Loading branch information
1 parent
d35b10b
commit ea8f09b
Showing
5 changed files
with
240 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/DroidSolutions.Oss.AuthClaimBinder/Exceptions/ClaimParsingException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace DroidSolutions.Oss.AuthClaimBinder.Exceptions; | ||
|
||
/// <summary> | ||
/// Special exception when a claim can not be parsed to the destination type. | ||
/// </summary> | ||
[Serializable] | ||
public class ClaimParsingException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
public ClaimParsingException() | ||
{ | ||
ClaimName = string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
/// <param name="name">The name of the claim.</param> | ||
/// <param name="type">The type that the claim should have been parsed to.</param> | ||
public ClaimParsingException(string name, Type? type) | ||
: this($"The claim \"{name}\" was not found.", null, name, type) | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
/// <param name="message">The message of the exception.</param> | ||
public ClaimParsingException(string? message) | ||
: this(message, (Exception?)null) | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
/// <param name="message">The exception message.</param> | ||
/// <param name="name">The name of the claim.</param> | ||
/// <param name="type">The type that the claim should have been parsed to.</param> | ||
public ClaimParsingException(string message, string name, Type? type) | ||
: this(message, null, name, type) | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
/// <param name="message">The exception message.</param> | ||
/// <param name="inner">The inner exception, if any.</param> | ||
public ClaimParsingException(string? message, Exception? inner) | ||
: base(message, inner) | ||
{ | ||
ClaimName = string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
/// <param name="message">The exception message.</param> | ||
/// <param name="inner">The inner exception, if any.</param> | ||
/// <param name="name">The name of the claim.</param> | ||
/// <param name="type">The type that the claim should have been parsed to.</param> | ||
public ClaimParsingException(string? message, Exception? inner, string name, Type? type) | ||
: base(message, inner) | ||
{ | ||
ClaimName = name; | ||
ClaimType = type; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClaimParsingException"/> class. | ||
/// </summary> | ||
/// <param name="info">Runtime serialization info.</param> | ||
/// <param name="context">Streaming context for serialization.</param> | ||
protected ClaimParsingException( | ||
SerializationInfo info, | ||
StreamingContext context) | ||
: base(info, context) | ||
{ | ||
ClaimName = info.GetString(nameof(ClaimName)) ?? string.Empty; | ||
|
||
string? typeName = info.GetString(nameof(ClaimType)); | ||
if (!string.IsNullOrEmpty(typeName)) | ||
{ | ||
ClaimType = Type.GetType(typeName); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the claim that could not be parsed. | ||
/// </summary> | ||
public string ClaimName { get; } | ||
|
||
/// <summary> | ||
/// Gets the type the claim should have been parsed to. | ||
/// </summary> | ||
public Type? ClaimType { get; } | ||
} |
78 changes: 78 additions & 0 deletions
78
src/DroidSolutions.Oss.AuthClaimBinder/Exceptions/MissingClaimException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace DroidSolutions.Oss.AuthClaimBinder.Exceptions; | ||
|
||
/// <summary> | ||
/// Special exception when a claim that is bound via model binder can not be found. | ||
/// </summary> | ||
[Serializable] | ||
public class MissingClaimException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingClaimException"/> class. | ||
/// </summary> | ||
public MissingClaimException() | ||
{ | ||
ClaimName = string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingClaimException"/> class. | ||
/// </summary> | ||
/// <param name="name">The name of the missing claim.</param> | ||
public MissingClaimException(string name) | ||
: this($"The claim \"{name}\" was not found.", name) | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingClaimException"/> class. | ||
/// </summary> | ||
/// <param name="message">The exception message.</param> | ||
/// <param name="name">The name of the missing claim.</param> | ||
public MissingClaimException(string message, string name) | ||
: base(message) | ||
{ | ||
ClaimName = name; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingClaimException"/> class. | ||
/// </summary> | ||
/// <param name="message">The exception message.</param> | ||
/// <param name="inner">The inner exception, if any.</param> | ||
public MissingClaimException(string? message, Exception? inner) | ||
: base(message, inner) | ||
{ | ||
ClaimName = string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingClaimException"/> class. | ||
/// </summary> | ||
/// <param name="message">The exception message.</param> | ||
/// <param name="inner">The inner exception, if any.</param> | ||
/// <param name="name">The name of the missing claim.</param> | ||
public MissingClaimException(string? message, Exception? inner, string name) | ||
: base(message, inner) | ||
{ | ||
ClaimName = name; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingClaimException"/> class. | ||
/// </summary> | ||
/// <param name="info">Runtime serialization info.</param> | ||
/// <param name="context">Streaming context for serialization.</param> | ||
protected MissingClaimException( | ||
SerializationInfo info, | ||
StreamingContext context) | ||
: base(info, context) | ||
{ | ||
ClaimName = info.GetString(nameof(ClaimName)) ?? string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the claim that was not found. | ||
/// </summary> | ||
public string ClaimName { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters