Exogenous shocks, contagion, and bank soundness: a macroeconomic framework