Keysmith

Error Handling

Sentinel errors for all Keysmith operations.

Keysmith defines sentinel errors in the root keysmith package. Use errors.Is to match them.

Sentinel errors

ErrorDescription
ErrInvalidKeyThe raw key is malformed or empty
ErrKeyNotFoundNo key matches the given hash or ID
ErrKeyExpiredThe key has passed its expiration time
ErrKeyRevokedThe key has been permanently revoked
ErrKeySuspendedThe key is temporarily suspended
ErrKeyRotatedThe key has been rotated and is outside the grace period
ErrKeyRateLimitedThe key has exceeded its rate limit
ErrPolicyViolationThe request violates the key's attached policy
ErrPolicyNotFoundNo policy matches the given ID
ErrScopeNotFoundNo scope matches the given ID
ErrInvalidTransitionThe requested state transition is not allowed
ErrDuplicateKeyA key with the same hash already exists
ErrMissingStoreNo store was provided to the engine
ErrMissingAppIDThe app ID is missing from context
ErrMissingTenantIDThe tenant ID is missing from context
ErrInvalidPrefixThe key prefix is invalid

Usage

import "errors"

vr, err := eng.ValidateKey(ctx, rawKey)
if err != nil {
    switch {
    case errors.Is(err, keysmith.ErrKeyNotFound):
        // Unknown key — return 401
    case errors.Is(err, keysmith.ErrKeyExpired):
        // Key expired — return 401
    case errors.Is(err, keysmith.ErrKeyRevoked):
        // Key revoked — return 403
    case errors.Is(err, keysmith.ErrKeySuspended):
        // Key suspended — return 403
    case errors.Is(err, keysmith.ErrKeyRateLimited):
        // Rate limited — return 429
    default:
        // Internal error — return 500
    }
}

Policy violation errors

When a key has an attached policy, the engine checks the policy constraints during validation. Policy violations produce ErrPolicyViolation which wraps a more specific message:

if errors.Is(err, keysmith.ErrPolicyViolation) {
    // err.Error() contains details like:
    // "policy violation: IP 203.0.113.5 not in allowlist"
    // "policy violation: rate limit exceeded (1000/min)"
}

On this page