generalize handle response function