HH-47815 fix bug in write_error and add tests