Check error before defer response body close