ShieldError class.
Error Classes
ShieldError
Base error class for all Shield errors. Extends the standard JavaScriptError class.
Machine-readable error code for programmatic handling
Error name (always
"ShieldError" for base class)Human-readable error message (inherited from
Error)InjectionDetectedError
Thrown when a prompt injection is detected in user input andonDetection: "block" is configured (default behavior).
Extends ShieldError.
Always
"INJECTION_DETECTED"Always
"InjectionDetectedError"Highest risk level among all detected injection patterns
Array of injection categories that were detected (e.g.,
["instruction_override", "role_hijack"])Formatted message like:
"Prompt injection detected (high risk): instruction_override, role_hijack"LeakDetectedError
Thrown when a system prompt leak is detected in model output andthrowOnLeak: true is configured.
Extends ShieldError.
Always
"LEAK_DETECTED"Always
"LeakDetectedError"Confidence score (0-1) for the leak detection. Higher values indicate stronger evidence of leakage.
Number of leaked fragments detected from the system prompt
Formatted message like:
"System prompt leak detected (confidence: 87%, 3 fragments)"Error Handling
Shield errors can be caught and handled using standard JavaScript error handling:Error Inheritance Hierarchy
catch (error instanceof ShieldError) block, or you can handle each error type individually for more granular control.
When Errors Are Thrown
InjectionDetectedError
Thrown by provider wrappers when:- Injection is detected in user input
onDetectionis set to"block"(default)
onDetectionis set to"warn"(only callback is invoked)detectis set tofalse(detection is disabled)
LeakDetectedError
Thrown by provider wrappers when:- A system prompt leak is detected in model output
throwOnLeakis set totrue
throwOnLeakisfalse(default) — output is silently redacted insteadsanitizeis set tofalse(sanitization is disabled)