Skip to content

Include frame ID in text decoding error messages #639

Closed

Description

@NebuPookins

When an ID3v2 text frame contains malformed UTF-16 content (e.g. odd byte length), lofty surfaces an error like: Text decoding: UTF-16 string has an odd length

I'm not very familiar with Rust, but I think the frame ID is available in parse_frame (in src/id3/v2/frame/read.rs) when parse_content fails, but is dropped by the ? operator:

fn parse_frame<R: Read>(
  reader: &mut R,
  size: u32,
  id: FrameId<'static>,
  ...
) -> Result<ParsedFrame<'static>> {
  match parse_content(reader, id, flags, version, parse_mode)? {  // id lost on error

The fix might be something like adding context to the error before propagating:

  parse_content(reader, id.clone(), flags, version, parse_mode)
  .map_err(|e| e.context(format!("In frame '{}'", id)))?

This would produce a message like:

Text decoding: UTF-16 string has an odd length (in frame 'TIT2')

..which makes it much easier for applications and end users to identify and fix the problematic tag without having to reimplement raw ID3 byte scanning themselves.

Activity

  1. added
    enhancement
    on Apr 4, 2026
  2. Serial-ATA commented on Apr 4, 2026

    @Serial-ATA
    Owner

    Yeah, there's lots of room for improvement in Lofty's errors. Especially in TextDecode, TooMuchData, SizeMismatch, and BadAtom.

    When I get some time I'll look into refactoring Error to hold a chain of errors instead, so you could get a FrameParse error that wraps a child TextDecode error.

    That'd make messages like this possible:

    Failed to parse 'TIT2' frame
    
    Caused by: UTF-16 string has an odd length
    
  3. Serial-ATA commented on Apr 19, 2026

    @Serial-ATA
    Owner

    @NebuPookins I've been experimenting with retaining error sources on the errors branch: https://github.com/Serial-ATA/lofty-rs/tree/errors

    So far only Id3v2Tag, Id3v1Tag, and MpegFile parsing have full error traces. It's a huge refactor, so it'll probably be awhile before I can get it out.

    You can now get nice error messages like:

    failed to parse Mpeg file
    Caused by: failed to parse ID3v2 tag
    Caused by: failed to parse frame 'TPE1'
    Caused by: failed to decode UTF-16 sequence: UTF-16 string has an invalid byte order mark
    

    With a basic reporter like this:

    fn report(mut err: &(dyn std::error::Error + 'static)) -> String {
    	use std::fmt::Write;
    	let mut s = format!("{err}");
    	while let Some(src) = err.source() {
    		let _ = write!(s, "\nCaused by: {src}");
    		err = src;
    	}
    	s
    }
    
    match Probe::open(path).and_then(Probe::read) {
    	Ok(_) => {
    		println!("Parsed file successfully!");
    	},
    	Err(e) => {
    		eprintln!("{}", report(&e));
    	},
    };
  4. added this to the 0.25.0 milestone on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions