handle error before deferring body close