labstack/echo · critical
invalid gzip level
Error message
invalid gzip level
What it means
Returned by GzipConfig.ToMiddleware when config.Level is outside the valid gzip level range [-2, 9], then converted to a panic by toMiddlewareOrPanic. gzip levels: -2 (HuffmanOnly), -1 (Default), 0 (no compression, remapped to -1), 1 (BestSpeed) through 9 (BestCompression). Any value outside this range is invalid.
Source
Thrown at middleware/compress.go:74
}
// Gzip returns a middleware which compresses HTTP response using gzip compression scheme.
func Gzip() echo.MiddlewareFunc {
return GzipWithConfig(GzipConfig{})
}
// GzipWithConfig returns a middleware which compresses HTTP response using gzip compression scheme.
func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
return toMiddlewareOrPanic(config)
}
// ToMiddleware converts GzipConfig to middleware or returns an error for invalid configuration
func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if config.Skipper == nil {
config.Skipper = DefaultSkipper
}
if config.Level < -2 || config.Level > 9 { // these are consts: gzip.HuffmanOnly and gzip.BestCompression
return nil, errors.New("invalid gzip level")
}
if config.Level == 0 {
config.Level = -1
}
if config.MinLength < 0 {
config.MinLength = 0
}
pool := gzipCompressPool(config)
bpool := bufferPool()
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
if config.Skipper(c) {
return next(c)
}
res := c.Response()View on GitHub (pinned to 05489dc173)
Solutions
- Use a valid level: -1 (default), 1-9 (speed to compression tradeoff), -2 (HuffmanOnly)
- Omit Level to accept the default (-1), or use middleware.Gzip() which uses defaults
- Use config.ToMiddleware() to get an error instead of a panic for graceful handling
Example fix
// before
mw := middleware.GzipWithConfig(middleware.GzipConfig{Level: 10}) // panic
// after
mw := middleware.GzipWithConfig(middleware.GzipConfig{Level: 6}) // ok Defensive patterns
Strategy: validation
Validate before calling
func validGzipLevel(level int) bool {
return level >= -2 && level <= 9
}
if !validGzipLevel(cfg.Level) {
return fmt.Errorf("invalid gzip level %d: must be -2..9", cfg.Level)
} Try / catch
defer func() {
if r := recover(); r != nil {
log.Fatal("middleware setup failed:", r)
}
}()
mw := middleware.GzipWithConfig(cfg) Prevention
- Use a valid gzip level: -1 (default), 1-9, or -2 (HuffmanOnly)
- Validate config values read from files/env before passing to middleware
- Use config.ToMiddleware() to get errors instead of panics during setup
When it happens
Trigger: Calling middleware.GzipWithConfig(middleware.GzipConfig{Level: 10}) or Level: -3 or any other out-of-range integer. The panic occurs at middleware chain construction time before the server starts.
Common situations: Typo or magic number for the level. Reading level from config file/env without validation. Confusing the level with MinLength or another setting.
Related errors
- echo basic-auth middleware requires a validator function
- echo body-dump middleware requires a handler function
- panic: err from config.ToMiddleware() in RequestLoggerWithCo
- timeout must be set
- at least one AllowOrigins is required or UnsafeAllowOriginFu
AI-assisted analysis of labstack/echo@05489dc173 (2026-08-04).
Data as JSON: /data/errors/d4f4cc00b8fe01cf.json.
Report an issue: GitHub.