This is still one of the singlehandedly most useful pieces of code I've run across in years. I have an Extended Events session watching for errors, severity 10 or higher. It logs them. I then use Tom's code below to figure out exactly where the error is. Flawless.
https://straightforwardsql.com/posts/investigating-errors-with-extended-events/ #sqlserver #extendedevents #xevents