Package com.tnsai.security
Class SecurityException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.tnsai.exceptions.TnsAIException
com.tnsai.security.SecurityException
- All Implemented Interfaces:
Serializable
Base exception for security-related violations.
This exception is thrown when security policies are violated, such as prompt injection detection, content moderation failures, or unauthorized access attempts.
Security Violation Types
| Type | Description | Retryable |
|---|---|---|
| PROMPT_INJECTION | Prompt injection detected | No |
| CONTENT_VIOLATION | Unsafe content detected | No |
| VALIDATION_FAILURE | Input validation failed | Yes |
| RATE_LIMITED | Rate limit exceeded | Yes |
| UNAUTHORIZED | Action not authorized | No |
| SANDBOX_VIOLATION | Sandbox policy violated | No |
| APPROVAL_REQUIRED | Human approval needed | Yes |
Usage Example
// Throw security exception
if (detector.detect(prompt).isInjectionDetected()) {
throw new SecurityException(
ViolationType.PROMPT_INJECTION,
"Potential prompt injection detected in user input"
);
}
// Handle security exception
try {
agent.chat(userInput);
} catch (SecurityException e) {
if (e.getViolationType() == ViolationType.RATE_LIMITED) {
// Wait and retry
Thread.sleep(e.getSuggestedRetryDelayMs());
agent.chat(userInput);
} else {
// Log and reject
auditLog.logViolation(e);
return "Request rejected due to security policy";
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumTypes of security violations. -
Constructor Summary
ConstructorsConstructorDescriptionSecurityException(SecurityException.ViolationType violationType, String message) Creates a security exception with type and message.SecurityException(SecurityException.ViolationType violationType, String message, String agentId, String actionName) Creates a security exception with context.SecurityException(SecurityException.ViolationType violationType, String message, Throwable cause) Creates a security exception with cause.SecurityException(SecurityException.ViolationType violationType, String message, List<String> details) Creates a security exception with details. -
Method Summary
Modifier and TypeMethodDescriptionGets the action name involved in the violation.Gets the agent ID involved in the violation.Gets additional details about the violation.longGets suggested retry delay in milliseconds.Gets the type of security violation.toString()static Optional<SecurityException> unauthorizedCause(Throwable failure) Locate a terminal authorization denial through executor/timeout wrappers.Methods inherited from class com.tnsai.exceptions.TnsAIException
getContext, getErrorCode, getMaxRetryAttempts, getMessage, getThrownAt, isRetryable, wrapMethods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
-
Constructor Details
-
SecurityException
Creates a security exception with type and message.- Parameters:
violationType- type of security violationmessage- error message
-
SecurityException
public SecurityException(SecurityException.ViolationType violationType, String message, List<String> details) Creates a security exception with details.- Parameters:
violationType- type of security violationmessage- error messagedetails- additional details about the violation
-
SecurityException
public SecurityException(SecurityException.ViolationType violationType, String message, String agentId, String actionName) Creates a security exception with context.- Parameters:
violationType- type of security violationmessage- error messageagentId- ID of the agent involvedactionName- name of the action attempted
-
SecurityException
public SecurityException(SecurityException.ViolationType violationType, String message, Throwable cause) Creates a security exception with cause.- Parameters:
violationType- type of security violationmessage- error messagecause- underlying cause
-
-
Method Details
-
getViolationType
Gets the type of security violation.- Returns:
- violation type
-
getDetails
Gets additional details about the violation.- Returns:
- list of details (never null)
-
getAgentId
Gets the agent ID involved in the violation.- Returns:
- agent ID or null if not available
-
getActionName
Gets the action name involved in the violation.- Returns:
- action name or null if not available
-
unauthorizedCause
Locate a terminal authorization denial through executor/timeout wrappers. -
getSuggestedRetryDelayMs
public long getSuggestedRetryDelayMs()Description copied from class:TnsAIExceptionGets suggested retry delay in milliseconds. Override in subclasses for specific retry strategies.- Overrides:
getSuggestedRetryDelayMsin classTnsAIException
-
toString
-