Improve error checking for nested writes.

Closes #2263.