Fix EZP-21664: switch to "error guards" approach to minimize code nesting