handle write and read error