Class TOTPException

java.lang.Object
java.lang.Throwable
java.lang.Exception
io.github.pratiyush.totp.TOTPException
All Implemented Interfaces:
Serializable

public final class TOTPException extends Exception
Exception thrown for TOTP-related errors.

This is a checked exception to ensure callers explicitly handle error cases. The exception is designed to prevent accidental leakage of sensitive information such as secrets or internal state.

Error Codes

Each exception includes an TOTPException.ErrorCode for programmatic error handling:

Security Note

Exception messages are sanitized to never include secrets or sensitive data. Always use the provided factory methods rather than constructing exceptions directly.

See Also:
  • Method Details

    • getErrorCode

      public TOTPException.ErrorCode getErrorCode()
      Returns the error code for this exception.
      Returns:
      the error code, never null
    • invalidSecret

      public static TOTPException invalidSecret(String reason)
      Creates an exception for invalid secrets.
      Parameters:
      reason - the reason the secret is invalid (no sensitive data!)
      Returns:
      new exception
    • invalidCode

      public static TOTPException invalidCode(String reason)
      Creates an exception for invalid codes.
      Parameters:
      reason - the reason the code is invalid
      Returns:
      new exception
    • invalidConfig

      public static TOTPException invalidConfig(String reason)
      Creates an exception for invalid configuration.
      Parameters:
      reason - the reason the configuration is invalid
      Returns:
      new exception
    • hmacError

      public static TOTPException hmacError(Throwable cause)
      Creates an exception for HMAC errors.
      Parameters:
      cause - the underlying cause
      Returns:
      new exception
    • qrGenerationError

      public static TOTPException qrGenerationError(Throwable cause)
      Creates an exception for QR generation errors.
      Parameters:
      cause - the underlying cause
      Returns:
      new exception
    • internalError

      public static TOTPException internalError(String message, Throwable cause)
      Creates an exception for internal errors.
      Parameters:
      message - the error message
      cause - the underlying cause
      Returns:
      new exception
    • toString

      public String toString()
      Overrides:
      toString in class Throwable