# Errors Every failure is a subclass of `CryptographySuiteError` with a stable `ErrorCode`. Programs branch on the code, never on the message. Messages are fixed per code and never contain paths, key material, plaintext, context values, or text from a key service. ```python from cryptography_suite import ( AuthenticationError, ContextMismatchError, CryptographySuiteError, EncryptionContext, Envelope, ErrorCode, Policy, Protector, ) from cryptography_suite.providers.hpke import HpkeRecipientProvider, generate_private_key provider = HpkeRecipientProvider() key = provider.add_identity("k", generate_private_key()) protector = Protector(provider=provider, policy=Policy(), primary_key=key) try: protector.open(Envelope(b"not an envelope"), context=EncryptionContext()) except (AuthenticationError, ContextMismatchError): raise # tampering or the wrong context: alert, do not retry except CryptographySuiteError as error: assert error.code is ErrorCode.FORMAT_INVALID print(error, error.retryable) # The envelope format is invalid. False ``` `retryable` is `True` only for transient key-service failures. The SDK has already retried those within the policy's budget before raising, so retrying again belongs in a job queue with backoff, not in a tight loop. `details` holds at most a retry count and an HTTP status code; every other value is redacted. ## Codes | Code | Class | CLI exit | Meaning | What to do | | --- | --- | --- | --- | --- | | `FORMAT_INVALID` | `EnvelopeError` | 3 | Not a well-formed envelope, or truncated. | Check that the right bytes were stored and read back completely. | | `FORMAT_UNSUPPORTED` | `EnvelopeError` | 3 | A version, profile, flag, or suite this reader does not support. | Upgrade the reader. | | `LIMIT_EXCEEDED` | `EnvelopeError` | 3 | A size or count above the policy's limit. | Use streaming, or raise the limit in the policy. | | `CRITICAL_FIELD_UNSUPPORTED` | `EnvelopeError` | 3 | The envelope requires a feature this reader lacks. | Upgrade the reader. | | `TRAILING_DATA` | `EnvelopeError` | 3 | Bytes after the final record. | Treat as tampering or a storage error. | | `AUTHENTICATION_FAILED` | `AuthenticationError` | 4 | The envelope was changed, or no trusted key opens it correctly. | Treat as tampering or corruption. Never retry. | | `CONTEXT_MISMATCH` | `ContextMismatchError` | 4 | The context differs from the one sealed. Same message as an authentication failure. | Check the context the caller supplies. | | `POLICY_INVALID` | `PolicyError` | 2 | A policy or configuration is invalid, such as an enterprise policy without an audit sink. | Fix the configuration. | | `POLICY_DENIED` | `PolicyError` | 5 | The policy forbids the operation: a provider, an algorithm, a missing context key, or a migration without a confirmed backup. | Fix the request or the policy. | | `KEY_STATE_DENIED` | `PolicyError` | 5 | The key version is disabled or destroyed. | When opening, another recipient is tried first. Re-enable the key or recover from escrow. | | `LEGACY_DENIED` | `PolicyError` | 5 | The policy does not permit migrating this v3 format, or an unencrypted v3 private key without `pem-der-unencrypted`, or its migration deadline has passed. | Use `Policy.migration` with the format and a deadline. | | `PROVIDER_UNAVAILABLE` | `ProviderError` | 7 | The key service failed or returned something unexpected. | Retried automatically when transient. Check the service. | | `PROVIDER_TIMEOUT` | `ProviderError` | 7 | The operation's deadline passed. | Retried automatically. Check latency and `provider_timeout_ms`. | | `PROVIDER_RATE_LIMITED` | `ProviderError` | 7 | The key service throttled the request. | Retried automatically. Raise the quota, or use hierarchical keys. | | `PROVIDER_AUTH_FAILED` | `ProviderError` | 6 | The key service refused the caller's credentials or permissions. | Check credentials and key permissions. | | `KEY_NOT_FOUND` | `ProviderError` | 6 | No recipient names a trusted key, or the key does not exist. | Add the key to the `Protector`, or to `decryption_keys`. | | `KEY_VERSION_STALE` | `ProviderError` | 6 | The key service answered for a different version than requested. | Check the key configuration. | | `CAPABILITY_UNSUPPORTED` | `ProviderError` | 6 | The key cannot do this: wrap with a public-only key, a wrong key type (including a v3 key an HPKE recipient cannot use), quorum shares still missing. | Use a key with the capability. | | `MIGRATION_VERIFY_FAILED` | `MigrationError` | 10 | The migrated envelope did not reopen to the same plaintext; nothing was published. | Check that the protector can open what it seals, and report it. | | `MIGRATION_INCOMPLETE`, `MIGRATION_CONFLICT` | `MigrationError` | 10 | Reserved for a resumable migration store (RFC-0033). | | | `IO_FAILED` | `OperationalError` | 8 | Reading, writing, or committing failed, or a required audit record could not be written. | Check storage and the audit sink. | | `OUTPUT_EXISTS` | `OperationalError` | 8 | The destination file exists. | Choose another name, or allow overwriting. | | `CANCELLED` | `OperationalError` | 9 | The `cancel` callback, a signal, or `--timeout` stopped the operation. Output was rolled back. | | | `INTERNAL_ERROR` | `OperationalError` | 70 | An unexpected failure, redacted. | Report it, with the operation identifier from the audit event. | The command line adds exit code 2 with code `USAGE_INVALID` for invalid arguments or configuration files. Arguments of the wrong Python type raise `TypeError`, and values outside their documented range raise `ValueError`, before any work starts: these are programming errors, not runtime conditions.