Fix error handling for write.