JuliusBrussee/caveman · error

unknown compression codec

Error message

unknown compression codec %q

What it means

Compress was called with a codec string it does not recognize. The library only supports its named constants (none/"", gzip, zstd) and fails closed on anything else rather than silently storing uncompressed data. This is an argument-validation error; the %q formatting shows the offending codec value.

Solutions

  1. Log/inspect the %q value in the error to see the exact offending codec string.
  2. Use the library's exported constants (CompressionGzip, CompressionZstd, CompressionNone) instead of raw string literals.
  3. Normalize user-supplied codec names (trim, lowercase) and map them to constants before calling.
  4. Add a validation step at config/ingest time that rejects unknown codec names early.
  5. Check stored records for legacy codec spellings and migrate them to supported values.

Example fix

// before
objectstore.Compress(cfg.Compression, data) // cfg.Compression = "GZIP" from config
// after
switch strings.ToLower(cfg.Compression) {
case "gzip":
    cfg.Compression = objectstore.CompressionGzip
case "zstd":
    cfg.Compression = objectstore.CompressionZstd
case "", "none":
    cfg.Compression = objectstore.CompressionNone
default:
    return fmt.Errorf("unsupported compression setting %q", cfg.Compression)
}
out, err := objectstore.Compress(cfg.Compression, data)
Defensive patterns

Strategy: validation

Validate before calling

var supported = map[string]bool{
    objectstore.CompressionGzip: true,
    objectstore.CompressionZstd: true,
    objectstore.CompressionNone: true,
    "":                          true,
}
if !supported[codec] {
    return fmt.Errorf("unsupported compression codec %q", codec)
}
out, err := objectstore.Compress(codec, data)

Try / catch

if err != nil {
    var uv *fmt.Errorf // inspect the %q codec name in the message
    return fmt.Errorf("config: compression codec rejected by objectstore: %w", err)
}

Prevention

When it happens

Trigger: Calling objectstore.Compress (or Decompress) with a codec string that is not one of CompressionNone, "", CompressionGzip, or CompressionZstd — e.g. a typo like "gzp", "GZIP" (case-sensitive), a value from user input, or a codec read from untrusted metadata.

Common situations: Storing the codec in a database/config with a different casing or spelling, an older client writing new codec names a newer build removed (or vice versa), or user-supplied compression preferences not validated before use.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/4ca9b7a381b89c51. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/objectstore/compress.go:52

// Compress encodes data with the named codec. "none" returns data unchanged.
func Compress(codec string, data []byte) ([]byte, error) {
	switch codec {
	case CompressionNone, "":
		return data, nil
	case CompressionGzip:
		var buf bytes.Buffer
		zw := gzip.NewWriter(&buf)
		if _, err := zw.Write(data); err != nil {
			return nil, fmt.Errorf("gzip: %w", err)
		}
		if err := zw.Close(); err != nil {
			return nil, fmt.Errorf("gzip close: %w", err)
		}
		return buf.Bytes(), nil
	case CompressionZstd:
		return zstdEnc.EncodeAll(data, nil), nil
	default:
		return nil, fmt.Errorf("unknown compression codec %q", codec)
	}
}

// Decompress reverses Compress with a defensive default expansion ceiling. An
// unknown codec fails closed.
func Decompress(codec string, data []byte) ([]byte, error) {
	return DecompressLimit(codec, data, DefaultMaxDecompressedBytes)
}

// DecompressLimit reverses Compress while refusing to produce more than
// maxBytes of plaintext. The limit is applied while decoding gzip and through
// zstd's decoder memory bound, so highly-compressible hostile payloads cannot
// first allocate their full expanded size and only then be rejected.
func DecompressLimit(codec string, data []byte, maxBytes int64) ([]byte, error) {
	if maxBytes < 0 || maxBytes > DefaultMaxDecompressedBytes {
		return nil, fmt.Errorf("invalid decompression limit %d", maxBytes)
	}
	switch codec {

View on GitHub (pinned to 3ee70a1026)