Update trap-error to handle cases where the handler expression may have side-effects