Handle errors and do conditional debugging