JSFiddle - React, Tailwind, and code Playground

by nirus

JavaScript

window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {		
		console.log(errorObj);
}

window.addEventListener("reportOnError", function(e){	
	console.log(e.detail);    
  /*Send to the server or any listeners for analysis.*/  
  //Http.send(e.detail);  
});

function ExceptionReport(ex, args, scope) {
	var self = {};
   self.message = ex.message;
   self.stack = ex.stack;
   self.name = ex.name;
   self.whoCalled = args.callee.caller.name == "" ? "Window": args.callee.caller.name;
   self.errorInFunction = args.callee.name;
   self.instanceOf = scope.constructor;
   /*Contains the parameters value set during runtime*/
   self.KeyPairValues = getParamNames(arguments.callee.caller.toString(), Array.prototype.slice.call(args));   
   window.dispatchEvent(new CustomEvent('reportOnError', {'detail':self}));
}

//Utilties 
function getParamNames(fnBody, values) {
  var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
   		ARGUMENT_NAMES = /([^\s,]+)/g,
  		result = fnBody.slice(fnBody.indexOf('(')+1, fnBody.indexOf(')')).match(ARGUMENT_NAMES),
  		obj={};
      
  fnBody.replace(STRIP_COMMENTS, '');  
  if(result !== null){  	
    	for(var i=0; i < result.length; i++){
      	obj[result[i]] = values.length !==0 ? values[i] : null;
      }    
  }else{
  	obj = null;
  }  
  return obj;
}
 
/*
	This is a testing/sample function that throws the error
*/
function testing(a,b,c){
	try{  
		dummy(1,2) ; //This line throws the error as reference error.
  }catch(e){  	
		ExceptionReport(e, arguments, this);
  }
}

//Class Emulation: For instanceof illustration.
function testingClass(){
	this.testing = testing;
}

//Named self executing function: This calls the function
var myvar = (function myvar(){
	testing(1,2,3);
})();

//Illustrating instanceof in exception 
var myVar2 = new testingClass();
myVar2.testing(1,2,3);

//Calling from global scope this is Window
testing(1,2,3);


//Without varialbles
testing();