Before an action can be commenced by a plaintiff, other than the state, or the municipal or public corporation named in the bond, leave shall be obtained of the court or judge thereof where the action is triable. Such leave shall be granted upon the production of a certified copy of the bond and an affidavit of the plaintiff, or some person in his or her behalf, showing the delinquency. But if the matter set forth in his or her affidavit be such that, if true, the party applying would clearly not be entitled to recover in the action, the leave shall not be granted. If it does not appear from the complaint that the leave herein provided for has been granted, the defendant, on motion, shall be entitled to judgment of nonsuit; if it does, the defendant may controvert the allegation, and if the issue be found in his or her favor, judgment shall be given accordingly.