rearrange error logic