This provides better error handling, and prevents errors like KeyError from bubbliing up to code in odd places. Fixes #17482