Share callback ending logic between error and close