Detecting and Mitigating Test-time Failure Risks via Model-agnostic Uncertainty Learning