-
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.
- Loading branch information
1 parent
2245aa0
commit f406a5b
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/com/corbado/enums/exception/ValidationErrorType.java
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,44 @@ | ||
package com.corbado.enums.exception; | ||
|
||
import com.corbado.exceptions.TokenValidationException; | ||
|
||
/** Enum representing error types for {@link TokenValidationException}. */ | ||
public enum ValidationErrorType { | ||
|
||
/** The invalid token. */ | ||
INVALID_TOKEN("Invalid token"), | ||
|
||
/** The signing key error. */ | ||
SIGNING_KEY_ERROR("Could not retrieve signing key"), | ||
|
||
/** The empty session token. */ | ||
EMPTY_SESSION_TOKEN("Session token is empty"), | ||
|
||
/** The empty issuer. */ | ||
EMPTY_ISSUER("Issuer is empty"), | ||
|
||
/** The issuer missmatch. */ | ||
ISSUER_MISSMATCH("Token issuer does not match"); | ||
|
||
/** The description. */ | ||
private final String description; | ||
|
||
/** | ||
* Instantiates a new validation error type. | ||
* | ||
* @param description the description | ||
*/ | ||
ValidationErrorType(final String description) { | ||
this.description = description; | ||
} | ||
|
||
/** | ||
* To string. | ||
* | ||
* @return the string | ||
*/ | ||
@Override | ||
public String toString() { | ||
return description; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/corbado/exceptions/TokenValidationException.java
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,67 @@ | ||
package com.corbado.exceptions; | ||
|
||
import com.corbado.enums.exception.ValidationErrorType; | ||
import lombok.Getter; | ||
|
||
/** | ||
* TokenValidationException is a custom exception used to indicate issues during token validation. | ||
* This exception is typically thrown when the token's issuer, format, or other key attributes do | ||
* not meet the expected validation criteria. | ||
* | ||
* <p>This exception wraps a specific {@link ValidationErrorType} enum value to categorize the type | ||
* of validation error encountered, and it provides a descriptive message detailing the reason for | ||
* the exception. Additionally, it can wrap an underlying exception that caused the validation | ||
* failure, allowing for more detailed error tracing. | ||
* | ||
* <p>Typical usage includes handling invalid token attributes, such as missing or mismatched | ||
* issuers, empty tokens, or other validation failures. | ||
* | ||
* <p>Example usage: | ||
* | ||
* <pre>{@code | ||
* try { | ||
* validateToken(token); | ||
* } catch (SomeOtherException e) { | ||
* throw new TokenValidationException(ValidationErrorType.ISSUER_MISMATCH, | ||
* "Issuer mismatch (configured issuer: 'expected.com', JWT issuer: 'actual.com')", e); | ||
* } | ||
* }</pre> | ||
* | ||
* @see ValidationErrorType | ||
*/ | ||
public class TokenValidationException extends RuntimeException { | ||
|
||
/** The Constant serialVersionUID. */ | ||
@Getter private static final long serialVersionUID = -2978676337061777870L; | ||
|
||
/** The error type. */ | ||
@Getter private final ValidationErrorType errorType; | ||
|
||
/** | ||
* Constructs a new TokenValidationException with a specified error type, a detailed message, and | ||
* an optional original exception that caused this validation failure. | ||
* | ||
* @param errorType the type of validation error encountered, represented by {@link | ||
* ValidationErrorType} | ||
* @param message a descriptive message providing additional context for the validation error | ||
* @param cause the original exception that caused this validation failure, if any | ||
*/ | ||
public TokenValidationException( | ||
final ValidationErrorType errorType, final String message, final Throwable cause) { | ||
super(message, cause); | ||
this.errorType = errorType; | ||
} | ||
|
||
/** | ||
* Constructs a new TokenValidationException with a specified error type and detailed message, | ||
* without an underlying cause. | ||
* | ||
* @param errorType the type of validation error encountered, represented by {@link | ||
* ValidationErrorType} | ||
* @param message a descriptive message providing additional context for the validation error | ||
*/ | ||
public TokenValidationException(final ValidationErrorType errorType, final String message) { | ||
super(message); | ||
this.errorType = errorType; | ||
} | ||
} |
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