{"record":{"id":"f81f28421e426459","repo":"leptos-rs/leptos","slug":"valid-text-format-type-with-utf-8-comptabile-strin","errorCode":null,"errorMessage":"Valid text format type with utf-8 comptabile string","messagePattern":"Valid text format type with utf-8 comptabile string","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server_fn/src/lib.rs","lineNumber":827,"sourceCode":"    Text,\n}\n/// A trait for types with an associated content type.\npub trait ContentType {\n    /// The MIME type of the data.\n    const CONTENT_TYPE: &'static str;\n}\n\n/// Data format representation\npub trait FormatType {\n    /// The representation type\n    const FORMAT_TYPE: Format;\n\n    /// Encodes data into a string.\n    fn into_encoded_string(bytes: Bytes) -> String {\n        match Self::FORMAT_TYPE {\n            Format::Binary => STANDARD_NO_PAD.encode(bytes),\n            Format::Text => String::from_utf8(bytes.into())\n                .expect(\"Valid text format type with utf-8 comptabile string\"),\n        }\n    }\n\n    /// Decodes string to bytes\n    fn from_encoded_string(data: &str) -> Result<Bytes, DecodeError> {\n        match Self::FORMAT_TYPE {\n            Format::Binary => {\n                STANDARD_NO_PAD.decode(data).map(|data| data.into())\n            }\n            Format::Text => Ok(Bytes::copy_from_slice(data.as_bytes())),\n        }\n    }\n}\n\n/// A trait for types that can be encoded into a bytes for a request body.\npub trait Encodes<T>: ContentType + FormatType {\n    /// The error type that can be returned if the encoding fails.\n    type Error: Display + Debug;","sourceCodeStart":809,"sourceCodeEnd":845,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/server_fn/src/lib.rs#L809-L845","documentation":"Encoding::into_encoded_string serializes a payload whose FORMAT_TYPE is Format::Text by calling String::from_utf8 and panics if the bytes are not valid UTF-8. The library assumes any text-format codec (e.g. JSON, URL) always produces UTF-8, so a non-UTF-8 payload indicates a bug or corrupted data.","triggerScenarios":"Invoking into_encoded_string on a text-format codec when the Bytes argument came from binary data, a custom Encoder that returns non-UTF-8 output, or corrupted/transcoded bytes (e.g. Latin-1 encoded source).","commonSituations":"Custom server fn encoding registered with Format::Text but a serializer emitting raw binary; bytes read from a file/DB in a non-UTF-8 encoding; middleware re-encoding request bodies; mixing Binary and Text formats when piping through caches.","solutions":["Verify the serializer/Encoder used with the text format always emits UTF-8 (JSON, URL-encoded, etc.)","Convert the payload to UTF-8 before passing it to into_encoded_string, or use String::from_utf8 lossily on your side first","Switch the codec to Format::Binary (base64 path) if the data is inherently binary","Fix the source of non-UTF-8 bytes (file read encoding, database column charset)"],"exampleFix":"// before\nlet s = MyFormat::into_encoded_string(latin1_bytes); // panics\n// after\nlet bytes = Bytes::from(String::from_utf8_lossy(&latin1_bytes).into_owned());\nlet s = MyFormat::into_encoded_string(bytes);\n","handlingStrategy":"try-catch","validationCode":"fn is_valid_utf8(bytes: &[u8]) -> bool { std::str::from_utf8(bytes).is_ok() }\n// call before: assert!(is_valid_utf8(&bytes));","typeGuard":"fn as_utf8(bytes: Bytes) -> Option<String> {\n    String::from_utf8(bytes.into()).ok()\n}","tryCatchPattern":"// panic-based API: validate first; if unavoidable, isolate via catch_unwind\nlet result = std::panic::catch_unwind(|| MyTextFormat::into_encoded_string(bytes.clone()));\nmatch result {\n    Ok(s) => s,\n    Err(_) => MyBinaryFormat::into_encoded_string(bytes), // fall back to base64 path\n}","preventionTips":["Only use Format::Text codecs with UTF-8-emitting serializers","Normalize file/DB encodings to UTF-8 upstream","Prefer Format::Binary for any possibly-non-UTF-8 payload","Add a debug assertion is_valid_utf8 before encoding in tests"],"tags":["encoding","utf8","panic"],"backgroundTag":"invalid-utf8-sequence","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}