Full audit trail in production
When something goes south - it’s usually in the production environment - where you don’t have access to debug or profiling information.
Design you code, so it can produce an audit trail - a complete profile of states, sequences, data in and out. That should give you clues, when you try to do your code-scene forensics.
At least you’ll get some clues on how to reproduce the error in your development environment.