{"id":"d4f4cc00b8fe01cf","repo":"labstack/echo","slug":"invalid-gzip-level","errorCode":null,"errorMessage":"invalid gzip level","messagePattern":"invalid gzip level","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"middleware/compress.go","lineNumber":74,"sourceCode":"}\n\n// Gzip returns a middleware which compresses HTTP response using gzip compression scheme.\nfunc Gzip() echo.MiddlewareFunc {\n\treturn GzipWithConfig(GzipConfig{})\n}\n\n// GzipWithConfig returns a middleware which compresses HTTP response using gzip compression scheme.\nfunc GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {\n\treturn toMiddlewareOrPanic(config)\n}\n\n// ToMiddleware converts GzipConfig to middleware or returns an error for invalid configuration\nfunc (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) {\n\tif config.Skipper == nil {\n\t\tconfig.Skipper = DefaultSkipper\n\t}\n\tif config.Level < -2 || config.Level > 9 { // these are consts: gzip.HuffmanOnly and gzip.BestCompression\n\t\treturn nil, errors.New(\"invalid gzip level\")\n\t}\n\tif config.Level == 0 {\n\t\tconfig.Level = -1\n\t}\n\tif config.MinLength < 0 {\n\t\tconfig.MinLength = 0\n\t}\n\n\tpool := gzipCompressPool(config)\n\tbpool := bufferPool()\n\n\treturn func(next echo.HandlerFunc) echo.HandlerFunc {\n\t\treturn func(c *echo.Context) error {\n\t\t\tif config.Skipper(c) {\n\t\t\t\treturn next(c)\n\t\t\t}\n\n\t\t\tres := c.Response()","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/middleware/compress.go#L56-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"Typo or magic number for the level. Reading level from config file/env without validation. Confusing the level with MinLength or another setting.","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"],"exampleFix":"// before\nmw := middleware.GzipWithConfig(middleware.GzipConfig{Level: 10}) // panic\n\n// after\nmw := middleware.GzipWithConfig(middleware.GzipConfig{Level: 6}) // ok","handlingStrategy":"validation","validationCode":"func validGzipLevel(level int) bool {\n    return level >= -2 && level <= 9\n}\nif !validGzipLevel(cfg.Level) {\n    return fmt.Errorf(\"invalid gzip level %d: must be -2..9\", cfg.Level)\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        log.Fatal(\"middleware setup failed:\", r)\n    }\n}()\nmw := middleware.GzipWithConfig(cfg)","preventionTips":["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"],"tags":["middleware","gzip","compression","configuration","panic","startup"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}