1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| /**
* @author Sébastien DAMA
*
*/
String.prototype.repeat=function(val){
var s='',t=this.toString();
while(--val>=0) s+=t;
return s;
}
Tryable=function(){
/*tester chaque el de l'objet savoir si c'est une méthode*/
for(var el in this){
if(typeof this[el] == "function"){/*Modifier la méthode */
fncTry(this,el);
}
}
}
/*Encupsuler la fonction dans un try...catch*/
function fncTry(obj,fnc){
var fct=obj[fnc];/*<= référence locale à la méthode*/
/*on souhaite redéfinir la méthode dans un try...catch*/
try{
fct.apply(obj,arguments);
}/*En cas d'érreur*/
catch(e){
/*Si c'est la première*/
if(!e.stackCounter)
e.stackCounter=1;
else
e.stackCounter++;
/*générer le message à montrer à l'utilisateur*/
msg_err=' '.repeat(e.stackCounter-1);
msg_err+=(e.stackCounter>1)?'à cause de':'';
if(obj.constructor.name!="")
msg_err+="erreur obj "+obj.constructor.name;
else
msg_err+="Anonymous object,";
msg_err+=" sur la méthode "+fnc;
console.log(msg_err);
/*En cas de première intération
On en a plus d'info*/
if(e.stackCounter==1){
msg_err="à la ligne "+e.lineNumber;
if(e.description!=undefined)
msg_err+=","+e.description;
else
msg_err+=","+e;
console.log(msg_err);
}
/*On fait remonter l'erreur au catch supérieur*/
return true;
}
} |
Partager