Pumpkin-MC/Pumpkin · error · CompressionLevelError

Invalid compression Level

Error message

Invalid compression Level

What it means

`CompressionLevelError` is returned when a compression level outside the valid range (0-9 for zlib/deflate) is supplied to packet encoding configuration. It is a dedicated error type rather than an io::Error so callers can match on it specifically.

Solutions

  1. Set the compression level to an integer in 0-9 (0 = no compression, 9 = max).
  2. Clamp or validate the level at config load time before handing it to the encoder.
  3. Check the pumpkin server config file for a bad `compression_level` value.

Example fix

// before
encoder.set_compression_level(15)?;
// after
let level = level.clamp(0, 9);
encoder.set_compression_level(level)?;
Defensive patterns

Strategy: validation

Validate before calling

fn validate_compression_level(level: i32) -> Result<u8, String> {
    if (0..=9).contains(&level) { Ok(level as u8) } else { Err(format!("compression level {level} not in 0-9")) }
}

Try / catch

match encoder.enable_compression(level) {
    Ok(()) => {},
    Err(e) if e.downcast_ref::<CompressionLevelError>().is_some() => {
        eprintln!("bad compression level {level}, falling back to 6");
        encoder.enable_compression(6)?;
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling the packet encoder's compression setup (e.g. `enable_compression`/level setter) with a level below 0 or above 9, or from a config value parsed incorrectly.

Common situations: A typo'd or mis-scaled value in server configuration (e.g. 10 or 22 taken from another library's scale); copying a compression level from a different ecosystem with a different valid range.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Pumpkin-MC/Pumpkin@8d4639e25a (2026-09-09). Data as JSON: /api/errors/178a880585bf103e. Report an issue: GitHub.

Appendix: source

Thrown at crates/pumpkin-protocol/src/java/packet_encoder.rs:372

    let version_number = P::to_id(*version);
    if version_number == -1 {
        return Err(WritingError::UnsupportedVersion(*version));
    }
    write.write_var_int(&VarInt(version_number))?;
    packet.write_packet_data(write, version)
}

pub fn serialize_packet<P: ClientPacket + ?Sized>(
    packet: &P,
    version: &JavaMinecraftVersion,
) -> Result<Bytes, WritingError> {
    let mut packet_buf = Vec::new();
    write_packet(packet, version, &mut packet_buf)?;
    Ok(packet_buf.into())
}

#[derive(Error, Debug)]
#[error("Invalid compression Level")]
pub struct CompressionLevelError;

#[cfg(test)]
mod tests {
    use std::io::Read;

    use super::*;
    use crate::java::client::status::CStatusResponse;
    use crate::packet::MultiVersionJavaPacket;
    use crate::ser::{NetworkReadExt, NetworkWriteExt};
    use crate::{ClientPacket, ReadingError};
    use aes::Aes128;
    use cfb8::Decryptor as Cfb8Decryptor;
    use flate2::read::ZlibDecoder;
    use pumpkin_data::packet::clientbound::status::STATUS_RESPONSE;
    use pumpkin_macros::java_packet;
    use pumpkin_util::version::JavaMinecraftVersion;

View on GitHub (pinned to 8d4639e25a)