throw encodingerror instead of invalidmessage in appropriate places