Cleanup function if encoder encounters an error