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

JIRA issue: https://jira.ez.no/browse/EZP-21664

Done. 
Sorry for the huge diffs, but the main reason for the issue itself is to change the nesting.