WriteResponse to wrap the common write logic with error handling